python - 是否有更好/更快的方法来查询 twitter 以获取包含字符串的历史推文?
问题描述
因此,我试图获取自某年以来符合特定搜索条件的所有推文(目前我只是拉 X 条推文,看看这个数字是否能让我回到我感兴趣的那一年)。
搜索条件只是包含在推文中的字符串。以“苹果”为例。我也在尝试过滤掉 RT。
这显然将是数十万,可能更像是数百万条推文。
为了尝试实现这一点,我一直在使用 Tweepy Twitter API:
def getTweets(searchValue, numOfTweets):
i = 0
tweetStream = tweepy.Cursor(twitterAPI.search, q=searchValue, tweet_mode="extended").items(numOfTweets)
f = open('csvfile.csv','w')
while True:
try:
for tweet in tweetStream:
if (tweet.full_text[0:2] != 'RT'):
print('Tweets processed:' + str(i))
f.write(str(formatTweet(tweet.full_text)))
f.write(str(tweet.created_at) + '\n')
i += 1
#Break here when all tweets in the stream have been processed
break
except tweepy.TweepError:
print('Sleeping for 2 mins to maintain Twitter API connection... \n')
time.sleep(120)
f.close()
return True
问题是它运行得非常慢。每秒可能会发布 50 条推文。如果我需要返回几年,这意味着让我的程序运行数天/数周。
我以前没有使用过 Tweepy,所以我不确定实现我想要的最佳方式。我的代码是我在其他地方看到的几篇文章/答案的混搭,所以我确信我的方法可能存在几个问题。有什么建议么?
解决方案
您可能想查看此文档developer.twitter.com
推荐阅读
- java - Hibernate 多对多添加重复项
- javascript - Angular 如何存储未链接到变量的可观察对象?
- c# - 如何从 Windows.UI.Colors 中获取 Window.UI.Color?
- audio - 我如何知道哪些频谱图帧属于哪些音频样本?
- javascript - 将 JS 模块导入 Web 浏览器脚本
- r - dyn.load(dll_copy_file) 中的错误——通过 linux 系统构建 R 包时出错
- html - 如果在过滤数据时删除表格行,我是否也应该将其从未过滤的数据中删除?Angular - 模板驱动形式
- instagram - 不要在新的 Instagram url-api 上为位置 (?__a=1) (native_location_data) 工作分页
- css - 具有动态高度的 div 的 CSS 线性渐变
- python - 我现在正在使用 Automate the Boring Stuff With Python 中的 pyautogui 模块。我的代码一直告诉我 pyscreez 没有属性 locateOnWindow