[英]get tweets with specific status ids using tweepy
我列出了需要獲取的特定推文狀態ID。 tweepy文檔提供以下內容:
API.get_status(id)
Returns a single status specified by the ID parameter.
Parameters: id – The numerical ID of the status.
Return type: Status object
我不知道如何使用它或找到任何示例。 這甚至是對的嗎?
我的ID列表長2240項,看起來像這樣:
response_ids = [717289507981107201, 717289501337509888, ..., 716684885411237888]
這些ID是從我已經擁有的tweets的'in_response_to_status_id'字段中獲得的(我想將我擁有的tweets與響應它們而編寫的tweet相匹配)。
我基本上想寫類似
for id in response_ids:
tweet = API.get_status(id)
非常感謝您提供有關如何執行此操作的幫助,或有關是否可行的建議。
最好使用“ statuses_lookup”命令。 以下鏈接中的更多信息http://docs.tweepy.org/en/v3.5.0/api.html#API.statuses_lookup
在運行下面的程序之前,獲取使用者密鑰和令牌。
import tweepy
consumer_key = xxxx
consumer_secret = xxxx
access_token = xxxx
access_token_secret = xxxx
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
tweets = api.statuses_lookup(id_list) # id_list is the list of tweet ids
tweet_txt = []
for i in tweets:
tweet_txt.append(i.text)
認為我已經解決了。
盡管我最初在分頁錯誤方面遇到了一些問題,但是get_status
似乎確實是正確的用法。 為了解決另一個類似的問題 ,我破解了一些代碼來解決此問題:
def paginate(iterable, page_size):
while True:
i1, i2 = itertools.tee(iterable)
iterable, page = (itertools.islice(i1, page_size, None),
list(itertools.islice(i2, page_size)))
if len(page) == 0:
break
yield page
index = 0
for page in paginate(response_ids, 1):
result = api.get_status(response_ids[index])._json
index += 1
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.