使用 Tweepy 查找特定关键字的热门推文
Find trending tweets, with Tweepy, for a specific keyword
耐心等待,因为我还在学习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)
这就是我现在正在使用的..
谢谢!
发件人:https://dev.twitter.com/rest/public/search
It allows queries against the indices of recent or popular Tweets and behaves similarly to, but not exactly like the Search feature available in Twitter mobile or web clients, such as Twitter.com search. The Twitter Search API searches against a sampling of recent Tweets published in the past 7 days.
因此,你几乎已经得到了你想要的。这些实际上是特定 hashtag/topic.
的热门推文
https://dev.twitter.com/rest/reference/get/trends/place 只是为了获取特定位置的热门话题,但从你的问题来看,你似乎想按某个特定主题搜索,而不是当前 tr
如果您说“我希望能够看到特定主题标签的最热门推文”时我理解正确的话。你的意思是你想要的推文有某些最流行(流行)的标签。
嗯,想了想,我想到了一个主意。您可以使用 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 截断文本)。此外,您希望结合回复数量和推文的转发数量来获得最流行的推文。
希望你觉得它有用!
编码愉快!!!
耐心等待,因为我还在学习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)
这就是我现在正在使用的..
谢谢!
发件人:https://dev.twitter.com/rest/public/search
It allows queries against the indices of recent or popular Tweets and behaves similarly to, but not exactly like the Search feature available in Twitter mobile or web clients, such as Twitter.com search. The Twitter Search API searches against a sampling of recent Tweets published in the past 7 days.
因此,你几乎已经得到了你想要的。这些实际上是特定 hashtag/topic.
的热门推文https://dev.twitter.com/rest/reference/get/trends/place 只是为了获取特定位置的热门话题,但从你的问题来看,你似乎想按某个特定主题搜索,而不是当前 tr
如果您说“我希望能够看到特定主题标签的最热门推文”时我理解正确的话。你的意思是你想要的推文有某些最流行(流行)的标签。
嗯,想了想,我想到了一个主意。您可以使用 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 截断文本)。此外,您希望结合回复数量和推文的转发数量来获得最流行的推文。
希望你觉得它有用!
编码愉快!!!