![](/img/trans.png)
[英]Twitter API v2 How to get a list of all tweets instead of streaming them
[英]Twitter API V2 - Get all tweets/Feed from Twitter List
我有一个 Twitter 列表,我想用 Twitter API V2 研究帐户抓取此列表中出现的每条推文。 这是列表的链接: https ://mobile.twitter.com/i/lists/912241909002833921
以下查询为我提供了创建此列表的帐户的推文:
import tweepy
from twitter_authentication import bearer_token
import time
import pandas as pd
import time
client = tweepy.Client(bearer_token, wait_on_rate_limit=True)
start = time.time()
csu_tweets = []
for response in tweepy.Paginator(client.search_all_tweets,
query = f'from:wahl_beobacher -is:retweet lang:de',
user_fields = ['username', 'public_metrics', 'description', 'location'],
tweet_fields = ['created_at', 'geo', 'public_metrics', 'text'],
expansions = 'author_id',
start_time = '2020-01-01T00:00:00Z',
end_time = '2022-12-06T00:00:00Z'):
time.sleep(1)
csu_tweets.append(response)
end = time.time()
print(f"Scraping needed {(end - start)/60} minutes.")
print(len(csu_tweets))
但是我想从此列表中获取提要,那么我该如何更改查询呢?
在此先感谢您的帮助!
您可以使用Tweepy 文档中提到的get_list_tweets
方法,并从列表中的 100 条推文的一页开始:
import tweepy
from twitter_authentication import bearer_token
import time
import pandas as pd
import time
client = tweepy.Client(bearer_token, wait_on_rate_limit=True)
start = time.time()
csu_tweets = []
response = client.get_list_tweets(
id=912241909002833921,
user_fields=['username', 'public_metrics', 'description', 'location'],
tweet_fields=['created_at', 'geo', 'public_metrics', 'text'],
expansions='author_id',
max_results=100)
end = time.time()
print(f"Scraping needed {(end - start)/60} minutes.")
for res in response.data:
csu_tweets.append(res)
print(len(csu_tweets))
一旦这对您有用,您就可以像以前一样实现分页方法和查询。 请记住,此方法没有start_time
或end_time
作为参数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.