私が作成しているpython twitterクローラーの一環として、私は複数のユーザーを何度もクロールしないようにするために "ハッシュテーブル"を作成しようとしています。それは以下の通りです。しかし、私はいくつかの問題にぶつかっています。ユーザーNYTimesKrugmanでクロールを開始すると、一部のユーザーを複数回クロールしているようです。私がユーザーcleversallie(別の完全に独立したクロールで)でクロールを開始するとき、私は何度もユーザーをクロールしません。この行動への洞察は非常に高く評価されるだろう!私は、次のようなコードを書き換えてきたご提案をもとに"Hashtable" for Python Twitter Crawler
from BeautifulSoup import BeautifulSoup
import re
import urllib2
import twitter
start_follower = "cleversallie"
depth = 3
U = list()
api = twitter.Api()
def add_to_U(user):
U.append(user)
def user_crawled(user):
L = len(L)
for x in (0, L):
a = L[x]
if a != user:
return False
else:
return True
def turn_to_names(users):
names = list()
for u in users:
x = u.screen_name
names.append(x)
return names
def test_users(users):
new = list()
for u in users:
if (user_crawled):
new.append(u)
return new
def crawl(follower,in_depth): #main method of sorts
if in_depth > 0:
add_to_U(follower)
users = api.GetFriends(follower)
names = turn_to_names(users)
select_users = test_users(names)
for u in select_users[0:5]:
crawl(u, in_depth - 1)
crawl(start_follower, depth)
for u in U:
print u
print("Program done.")
EDIT (ありがとうございました!):
import re
import urllib2
import twitter
start_follower = "NYTimesKrugman"
depth = 4
searched = set()
api = twitter.Api()
def crawl(follower, in_depth):
if in_depth > 0:
searched.add(follower)
users = api.GetFriends(follower)
names = set([str(u.screen_name) for u in users])
names -= searched
for name in list(names)[0:5]:
crawl(name, in_depth-1)
crawl(start_follower, depth)
for x in searched:
print x
print "Program is completed."
Uは設定されていませんが、かなりクールに見えます。あなたはおそらく最後に "\ n" .join(検索)してください。 – koblas
あなたが少し提案したものを修正した後、上記の編集を思いついた。これまでに試したいくつかのテストケースで動作しているようです。 – snehoozle
@koblasうん、ありがとう。 –