问题
I am trying to go through tweets of a particular user and get all replies on that tweet. I found that the APIv1.1 of twitter does not directly support it.
Is there a hack or a workaround on getting the replies for a particular tweet. I am using python Streaming API.
回答1:
There is a workaround using the REST API.
You will need the id_str and @username of the author of the original tweet you want to find replies to.
You should use the Search API for the "@username" of the author. Go through the results looking for the 'in_reply_to_status_id' field to compare to the id_str of the specific tweet you want replies for.
回答2:
Here's a work around to fetch replies of a tweet made by "username" using the rest API using tweepy
1) Find the tweet_id of the tweet for which the replies are required to be fetched
2) Using the api's search method query the following (q="@username", since_id=tweet_id) and retrieve all tweets since tweet_id
3) the results matching the in_reply_to_status_id to tweet_id is the replies for the post.
回答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()
The above code will fetch 10 recent tweets of an user(name) along with the replies to that particular tweet.The replies will be saved on to a list named replies. You can retrieve more tweets by increasing the items count (eg:items(100)).
来源:https://stackoverflow.com/questions/29928638/getting-tweet-replies-to-a-particular-tweet-from-a-particular-user