1
我使用Tweepy
通過他們的推特ID從Twitter API收集推文。
我試圖讀取一個完整的ID文件,從對話流獲取以前的推文,然後將該推文及其作者的屏幕名稱等存儲在文本文件中。部分推文已被刪除或用戶的個人資料已設置爲私密,在這種情況下,我想忽略該推文並轉到下一個推文。但是,由於某些原因,我並未收集所有可訪問的推文。它的存儲可能是3/4的非專用推文,並且尚未被刪除。任何想法爲什麼它沒有捕捉到一切?Twitter API - 不收集使用Tweepy的所有推文
在此先感謝。
def getTweet(tweetID, tweetObj, callTweetObj, i):
tweet = callTweetObj.text.encode("utf8")
callUserName = callTweetObj.user.screen_name
callTweetID = tweetObj.in_reply_to_status_id_str
with open("call_tweets.txt", "a") as calltweets:
output = (callTweetObj.text.encode('utf-8')+ "\t" + callTweetID + "\t" + tweetID)
calltweets.write(output)
print output
with open("callauthors.txt", "a") as callauthors:
cauthors = (callUserName+ "\t" + "\t" + callTweetID + "\n")
callauthors.write(cauthors)
with open("callIDs.txt", "a") as callIDs:
callIDs.write(callTweetID + "\n")
with open("newResponseIDs.txt", "a") as responseIDs:
responseIDs.write(tweetID)
count = 0
file = "Response_IDs.txt"
with open(file, 'r+') as f:
lines = f.readlines()
for i in range(0, len(lines)):
tweetID = lines[i]
sleep(5)
try:
tweetObj = api.get_status(tweetID)
callTweetID = tweetObj.in_reply_to_status_id_str
callTweetObj = api.get_status(callTweetID)
getTweet(tweetID, tweetObj, callTweetObj, i)
count = count+1
print count
except:
pass
有沒有明顯的模式?你錯過了舊推文嗎?銳推?推文使用非英語語言?推文中有Unicode字符? – Chris
記錄捕獲的異常和失敗的原因將很有用。您目前正在丟棄數據(使用'except:pass')來調試您的代碼。 – msw
'Response_IDs.txt'從哪裏來,你爲什麼認爲它的內容是準確的? – msw