【问题标题】:Twitter API V2 - Get all tweets/Feed from Twitter ListTwitter API V2 - 从 Twitter 列表中获取所有推文/提要
【发布时间】:2022-12-22 02:27:41
【问题描述】:

我有一个 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))

但是我想从此列表中获取提要,那么我该如何更改查询呢?

在此先感谢您的帮助!

【问题讨论】:

    标签: python response tweepy twitter-api-v2


    【解决方案1】:

    您可以使用 Tweepy Docs 中提到的 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_timeend_time 作为参数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-31
      • 2012-12-05
      • 2013-06-14
      • 2020-12-11
      • 2012-12-12
      • 2022-10-05
      • 2016-06-21
      • 2021-03-28
      相关资源
      最近更新 更多