【问题标题】:Getting tweet replies to a particular tweet from a particular user从特定用户那里获取对特定推文的推文回复
【发布时间】:2015-07-07 20:21:02
【问题描述】:

我正在尝试浏览特定用户的推文并获得对该推文的所有回复。发现twitter的APIv1.1并没有直接支持。

在获取特定推文的回复方面是否有破解或解决方法。我正在使用 python Streaming API。

【问题讨论】:

    标签: python twitter tweepy tweets twitter-streaming-api


    【解决方案1】:

    有一个使用 REST API 的解决方法。

    您将需要要查找回复的原始推文作者的 id_str 和 @username。

    您应该使用Search API 作为作者的“@username”。查看结果,查找“in_reply_to_status_id”字段,与您想要回复的特定推文的 id_str 进行比较。

    【讨论】:

    • 你能写出你的编程方式吗?
    【解决方案2】:

    这是一种解决方法,可以使用 tweepy 使用其余 API 获取“用户名”发出的推文的回复

    1) 查找需要获取回复的推文的 tweet_id

    2) 使用 api 的搜索方法查询以下 (q="@username", since_id=tweet_id) 并检索自 tweet_id 以来的所有推文

    3) 匹配到 in_reply_to_status_id 到 tweet_id 的结果就是帖子的回复。

    【讨论】:

    • 你会如何编程?
    【解决方案3】:
    replies=[] 
    non_bmp_map = dict.fromkeys(range(0x10000, sys.maxunicode + 1), 0xfffd)  
    for full_tweets in tweepy.Cursor(api.user_timeline,screen_name=name,timeout=999999).items(10):
      for tweet in tweepy.Cursor(api.search,q='to:'+name,result_type='recent',timeout=999999).items(1000):
        if hasattr(tweet, 'in_reply_to_status_id_str'):
          if (tweet.in_reply_to_status_id_str==full_tweets.id_str):
            replies.append(tweet.text)
      print("Tweet :",full_tweets.text.translate(non_bmp_map))
      for elements in replies:
           print("Replies :",elements)
      replies.clear()
    

    上述代码将获取用户(姓名)最近的 10 条推文以及对该特定推文的回复。回复将保存到名为 replies 的列表中。您可以通过增加 items 计数来检索更多推文(例如:items(100))。

    【讨论】:

      【解决方案4】:

      即使经过这么多的方法和帮助,我还是花了大约一个小时找出确切的代码来获取对原作者发布的推文的回复。推特用户除了获取回复外,大多回复回复是为了做一个话题(这与获取原作者的整个话题不同)

      我最近一直在做一个简单的项目,它将原始作者线程中每条推文的屏幕截图上传到您的 Google 照片。能够将reply 获取到推文和reply to the replies 的最重要部分

      这是我写的一个简单的递归,它解决了我的问题。该函数将urls列表更新为所有回复和回复作者回复的URL。

      def update_urls(tweet, api, urls):
          tweet_id = tweet.id
          user_name = tweet.user.screen_name
          max_id = None
          replies = tweepy.Cursor(api.search, q='to:{}'.format(user_name),
                                      since_id=tweet_id, max_id=max_id, tweet_mode='extended').items()
      
          for reply in replies:
              if(reply.in_reply_to_status_id == tweet_id):
                  urls.append(get_twitter_url(user_name, reply.id))
                  try:
                      for reply_to_reply in update_urls(reply, api, urls):
                          pass
                  except Exception:
                      pass
              max_id = reply.id
          return urls
      

      如果您打算使用 update_urls 函数,以下是您可能需要的一些附加函数。

      def get_api():
          auth=tweepy.OAuthHandler(consumer_key, consumer_secret)
          auth.set_access_token(access_key, access_secret)
          api = tweepy.API(auth, wait_on_rate_limit=True)
          return api
      
      def get_tweet(url):
          tweet_id = url.split('/')[-1]
          api = get_api()
          tweet = api.get_status(tweet_id)
          return tweet
      
      def get_twitter_url(user_name, status_id):
          return "https://twitter.com/" + str(user_name) + "/status/" + str(status_id)
      

      运行确切的代码:

      api = get_api()
      tweet = get_tweet(url)
      urls = [url]
      urls = update_urls(tweet, api, urls)
      

      如果您想获取特定 URL 的内容,只需调用 get_tweet(url) 并使用推文对象获取 tweet.texttweet.user 等信息。让我知道它是否适合您:)

      【讨论】:

        【解决方案5】:

        以下函数使用用户名和 tweet_id 返回对特定 tweet_id 的所有回复文本列表:(我假设 api 已经在程序中声明。)

        def get_tweet_thread(username,tweet_id):
            replies = tweepy.Cursor(api.search, q='to:{}'.format(username),since_id=tweet_id, tweet_mode='extended').items()
        
            replied_thread = list()
            for reply in replies:
                if(reply._json['in_reply_to_status_id'] == tweet_id):
                     replied_thread.append(reply._json['full_text'])
                
            return(replied_thread)
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-06-24
          • 2016-10-20
          • 1970-01-01
          • 2018-02-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多