获取推文回复特定用户的特定推文

时间:2015-04-28 19:52:55

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

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

是否有针对特定推文的回复的黑客攻击或解决方法。我正在使用python Streaming API。

5 个答案:

答案 0 :(得分:12)

使用REST API有一种解决方法。

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

你应该使用Search API作为" @ username"作者浏览结果,查找' in_reply_to_status_id'要与您要回复的特定推文的id_str进行比较的字段。

答案 1 :(得分:6)

以下是使用tweepy使用其余API获取“username”所做推文的回复的工作

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

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

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

答案 2 :(得分:4)

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条最新推文以及对该特定推文的回复。回复将保存到名为回复的列表中。您可以通过增加计数来检索更多推文(例如:items(100))。

答案 3 :(得分:3)

即使经过如此多的方法和帮助,我仍然花了大约一个小时来弄清确切的代码,以获取对原始作者的推文的回复。除了获取答复之外,twitter用户主要对答复进行答复以创建线程(这使得获取原始作者创建的整个线程有所不同)

我最近一直在研究一个简单的项目,该项目将原始作者线程中每条推文的屏幕快照上传到您的Google相册。能够将reply获取到推文和reply to the replies

的最重要部分

这是我写的一个简单的递归,可以解决我的问题。此功能使用所有回复的URL和作者的回复更新urls列表。

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对象即可获取tweet.texttweet.user等信息。让我知道它是否对您有用:)

答案 4 :(得分:0)

以下函数使用用户名和 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)
相关问题