首页 > 解决方案 > 如何在 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

标签: pythontweepystatus

解决方案


推文 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'])

推荐阅读