【发布时间】:2016-06-14 05:32:49
【问题描述】:
我正在尝试使用 tweepy 从 twitter api 收集大量推文。我有一个包含大约一万条推文 ID 的文本文件。我的程序读取文件,抓取每条推文以及它正在回复的推文。然后它将每条推文的文本以及每个作者的用户名保存在各自的文本文件中。代码如下:
auth = tweepy.OAuthHandler(ckey, csecret)
auth.set_access_token(atoken, asecret)
api = tweepy.API(auth)
tweetsFile = open("srcstic.txt", "r")
tweets_seen = set() # holds tweets already seen
def getNextLine():
while True:
tweetID = tweetsFile.readline()
getTweetObj(tweetID)
if not tweetID:
break
def getTweetObj(tweetID):
try:
tweetObj = api.get_status(tweetID)
#sleep(11)
except tweepy.error.TweepError:
getNextLine()
else:
pass
tweet = tweetObj.text.encode("utf8")
idnum = tweetObj.in_reply_to_status_id_str
try:
former = api.get_status(idnum)
except tweepy.error.TweepError:
getNextLine()
else:
printFiles(former, tweetObj, tweet)
def printFiles(former, tweetObj, tweet):
callUserName = former.user.screen_name
responseUserName = tweetObj.user.screen_name
if tweet not in tweets_seen:
with open("callauthors.txt", "a") as callauthors:
cauthors = callUserName + "\n"
callauthors.write(cauthors)
with open("responseauthors.txt", "a") as responseauthors:
rauthors = responseUserName + "\n"
responseauthors.write(rauthors)
with open("response_tweets.txt", "a") as responsetweets:
output = (tweetObj.text.encode('utf-8')+"\n")
responsetweets.write(output)
with open("call_tweets.txt", "a") as calltweets:
output = (former.text.encode('utf-8')+"\n")
calltweets.write(output)
tweets_seen.add(tweet)
getNextLine()
但是,一段时间内一切正常,然后我收到以下错误:
File "gettweets2.py", line 68, in <module>
getNextLine()
File "gettweets2.py", line 21, in getNextLine
getTweetObj(tweetID)
File "gettweets2.py", line 40, in getTweetObj
getNextLine()
File "gettweets2.py", line 21, in getNextLine
getTweetObj(tweetID)
File "gettweets2.py", line 31, in getTweetObj
getNextLine()
File "gettweets2.py", line 21, in getNextLine
getTweetObj(tweetID)
File "gettweets2.py", line 31, in getTweetObj
getNextLine()
File "gettweets2.py", line 21, in getNextLine
getTweetObj(tweetID)
File "gettweets2.py", line 31, in getTweetObj
getNextLine()
........
........
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/_ abcoll.py", line 540, in update
if isinstance(other, Mapping):
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/abc.py", line 141, in __instancecheck__
subtype in cls._abc_negative_cache):
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/_weakrefset.py", line 73, in __contains__
return wr in self.data
RuntimeError: maximum recursion depth exceeded in cmp
任何想法这里出了什么问题? 谢谢。
【问题讨论】: