【问题标题】:How to get replies or quotes on a specific tweet如何获得特定推文的回复或引用
【发布时间】:2022-01-27 08:14:02
【问题描述】:

我在对象 obj 中获取了该用户的推文和相应的 id。我想知道为什么我没有得到像conversation_id 这样的其他信息。我想用它来获取回复和报价。这是我在互联网上找到的解决方案,但不知道如何使其工作。 有谁知道提取conversation_id 或任何其他参数,如geo.place_id?我正在使用 tweepy,但如果有人有任何其他解决方案使用另一个库来获得相同的结果,它也会有所帮助。谢谢你的帮助!!! 如果您创建另一个文件配置并定义您的令牌,您可以尝试该代码。出于安全考虑,我不能分享我的。

import tweepy
import config
users_name = ['derspiegel', 'zeitonline']
tweet_tab = []

def getClient():
    client = tweepy.Client(bearer_token=config.BEARER_TOKEN,
                           consumer_key=config.API_KEY,
                           consumer_secret=config.API_KEY_SECRET,
                           access_token=config.ACCESS_TOKEN,
                           access_token_secret=config.ACCESS_TOKEN_SECRET)

def searchTweets(client):
    for i in users_name:
        client = getClient()
        user = client.get_user(username=i)
        userId = user.data.id
        tweets = client.get_users_tweets(userId,
                                         expansions=[
                                             'author_id', 'referenced_tweets.id', 'referenced_tweets.id.author_id',
                                             'in_reply_to_user_id', 'attachments.media_keys', 'entities.mentions.username', 'geo.place_id'],
                                         tweet_fields=[
                                             'id', 'text', 'author_id', 'created_at', 'conversation_id', 'entities',
                                             'public_metrics', 'referenced_tweets'
                                         ],
                                         user_fields=[
                                             'id', 'name', 'username', 'created_at', 'description', 'public_metrics',
                                             'verified'
                                         ],
                                         place_fields=['full_name', 'id'],
                                         media_fields=['type', 'url', 'alt_text', 'public_metrics'])
        if not tweets is None and len(tweets) > 0:
            obj = {}
            obj['id'] = userId
            obj['text'] = tweets
            tweet_tab.append(obj)
    return tweet_tab


searchTweets(client)
print("tableau final", tweet_tab)


【问题讨论】:

    标签: python python-3.x twitter tweepy


    【解决方案1】:

    我的猜测是您需要将 id 放入一个列表中,函数可以通过该列表进行迭代。创建 id 列表并尝试:

    def get_tweets_from_timelines():
    
        tweets_timelines_list = []
        for id in range(0, len(ids), 1):
            one_id = (ids[id:id+1])
            one_id = ' '.join(one_id) 
    
        for tweet in tweepy.Paginator(client.get_users_tweets, id=one_id, max_results=100,
                tweet_fields=['attachments', 'author_id', 'context_annotations', 'created_at', 'entities', \
                    'conversation_id', 'possibly_sensitive', 'public_metrics', 'referenced_tweets', \
                     'reply_settings', 'source', 'withheld' ],\
                user_fields=['created_at', 'description', 'entities', 'profile_image_url', 'protected', \
                    'public_metrics', 'url', 'verified', 'withheld'],
                expansions=['referenced_tweets.id', 'in_reply_to_user_id', 'attachments.media_keys', ],
                media_fields=['preview_image_url'],
    
                         ): 
                tweets_timelines_list.append(tweet)
        return tweets_timelines_list
    

    【讨论】:

      猜你喜欢
      • 2016-10-20
      • 2015-07-07
      • 2021-06-24
      • 2015-09-10
      • 1970-01-01
      • 2018-02-25
      • 2017-05-21
      • 2017-11-10
      • 2015-06-14
      相关资源
      最近更新 更多