python - 如何在 Tweepy 中获取推文 ID
问题描述
我想搜索一个关键字并获得 70 条推文,然后列出所有推文的 ID。我不知道怎么做,请帮忙。
import tweepy
import time
auth = tweepy.OAuthHandler('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx','xxxxxxxxxxxxxxxxxxxxxxxxxx')
auth.set_access_token('xxxxxxxxxxxxxxxxxxxxxxx','xxxxxxxxxxxxxxxxxxxxxxxxxxx')
api = tweepy.API(auth, wait_on_rate_limit=True, wait_on_rate_limit_notify=True)
user = api.me()
import time
time_limit_sec = 600
start_time = time.time()
for tweet in tweepy.Cursor(api.search, q="books", result_type="recent").items(70):
status = api.get_status(id)
id = status.id
print("The ID of the status is :" + str(id))
break
解决方案
推文 ID 在 _json['id'] 下。不要使用break,它会在你设置 items(70) 时限制为 70。
for tweet in tweepy.Cursor(api.search, q="books", result_type="recent").items(70):
print("The tweet id is:", tweet._json['id'])
推荐阅读
- swift - 自定义 Swiftlint 规则,使 Xcode 警告(如折旧)成为错误
- c# - 如何在 asp.net 中使用本地化?
- ruby-on-rails - rails openssl 来自代码和终端的不同加密结果
- c++ - 如何判断 `constexpr` 是否在编译时评估(无需手动检查)
- swift - Swift - 使用具体实现时类型不符合协议
- spring - 无法启动嵌入式 Tomcat NoClassDefFoundError DispatcherServletPath
- javascript - 如何访问javascript数组中的项目“橙子”
- reactjs - 如何在项目中全局保存任何值,以便我们可以从 react.js 中的任何位置访问它
- php - 防止 Web 应用程序中 PHP 文件的直接 URL 访问
- php - PHP使用单词中的字符以正确的顺序输出相应的段落