4

请耐心等待,因为我还在学习 Python ..

我希望能够看到特定主题标签的最热门推文。我能够找到趋势推文,并且能够找到带有特定主题标签的推文,但是在尝试将这两者结合起来时我不知所措。

import tweepy

CONSUMER_KEY = 'key'
CONSUMER_SECRET = 'secret'
OAUTH_TOKEN = 'key'
OAUTH_TOKEN_SECRET = 'secret'

auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(OAUTH_TOKEN, OAUTH_TOKEN_SECRET)

api = tweepy.API(auth)
trends = api.trends_place(1)

search_hashtag = tweepy.Cursor(api.search, q='hashtag').items(5000)

for tweet in search_hashtag:
    print json.dumps(tweet)

print json.dumps(trends, indent=1)

这就是我现在正在工作的..

谢谢!

4

2 回答 2

1

来自:https ://dev.twitter.com/rest/public/search

它允许对最近或流行推文的索引进行查询,其行为类似于但不完全类似于 Twitter 移动或 Web 客户端中可用的搜索功能,例如 Twitter.com 搜索。Twitter 搜索 API 根据过去 7 天内发布的最新推文样本进行搜索。

因此,你几乎已经得到了你想要的。这些实际上是特定主题标签/主题的热门推文。

https://dev.twitter.com/rest/reference/get/trends/place只是为了获取特定位置的热门话题,但从您的问题来看,您似乎想要搜索某个特定主题,而不是那个目前 tr

于 2017-05-01T14:36:53.803 回答
0

如果我在您说“我希望能够看到特定主题标签的最热门推文”时理解正确。您的意思是您想要具有某些最流行(流行)的主题标签的推文。

好吧,想了想,我想到了一个主意。您可以使用 hastag 作为查询,然后在收集的推文中搜索转发次数最多的推文。

代码可能如下所示:

import tweepy

CONSUMER_KEY = 'key'
CONSUMER_SECRET = 'secret'
OAUTH_TOKEN = 'key'
OAUTH_TOKEN_SECRET = 'secret'

auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(OAUTH_TOKEN, OAUTH_TOKEN_SECRET)

api = tweepy.API(auth)
trends = api.trends_place(1)

search_hashtag = tweepy.Cursor(api.search, q='hashtag', tweet_mode = "extended").items(5000)

for tweet in search_hashtag:
        if 'retweeted_status' in tweet._json: 
            full_text = tweet._json['retweeted_status']['full_text']
        else:
            full_text = tweet.full_text
                        
        tweets_list.append([tweet.user.screen_name,
                            tweet.id,
                            tweet.retweet_count, # What you are interested of
                            tweet.favorite_count, # Maybe it is helpfull too
                            full_text,
                            tweet.created_at,
                            tweet.entities
                           ])
tweets_df = pd.DataFrame(tweets_list, columns = ["screen_name", "tweet_id",
                                                  "nº rt", 
                                                  "nº replies",
                                                  "text",
                                                  "created_at", 
                                                  "entities"])
# In a data frame format you can sort the tweets by the nº of rt and get the top one
tweets_df.to_json()

如您所见,我使用tweet_mode = "extended"了可能是因为您可能对整个文本感兴趣(默认情况下,Twitter API 会截断文本)。此外,您希望使用回复数与推文的转发数相结合来获得最时尚。希望你觉得它有用!编码快乐!!!

于 2020-10-10T19:16:08.727 回答