Twitter API:如何根据查询词和预定时间跨度+推文特征搜索推文
Twitter API: How to search tweets based on query words and predetermined time span + tweets characteristics
新手程序员在这里寻求帮助。我有一个主题标签列表,我想为其获取从 01-01-2015 到 31-12-2018 的所有历史推文。
我尝试使用 Tweepy 库,但它只允许访问最近 7 天的推文。我还尝试使用 GetOldTweets,因为它可以访问历史推文,但它一直在崩溃。所以现在我已经获得了 Twitter 的高级 API 访问权限,这也让我可以访问完整的历史推文。
为了使用 premium API 进行查询,我不能使用 Tweepy 库(因为它没有 link 和 premium APIs 对吗?)我的选择是 TwitterAPI 和 Search-Tweets.
1- TwitterAPI 和 Search-Tweets 是否提供有关用户名、用户位置、用户是否经过验证、推文语言、推文来源、计数等信息转推和收藏以及每条推文的日期? (就像 tweepy 那样)。我找不到有关此的任何信息。
2- 我可以在查询中提供时间跨度吗?
3- 我该怎么做所有这些?
这是我的 Tweepy 库代码:
hashtags = ["#AAPL","#FB","#KO","#ABT","#PEPCO",...]
df = pd.DataFrame(columns = ["Hashtag", "Tweets", "User", "User_Followers",
"User_Location", "User_Verified", "User_Lang", "User_Status",
"User_Method", "Fav_Count", "RT_Count", "Tweet_date"])
def tweepy_df(df,tags):
for cash in tags:
i = len(df)+1
for tweet in tweepy.Cursor(api.search, q= cash, since = "2015-01-01", until = "2018-12-31").items():
print(i, end = '\r')
df.loc[i, "Hashtag"] = cash
df.loc[i, "Tweets"] = tweet.text
df.loc[i, "User"] = tweet.user.name
df.loc[i, "User_Followers"] = tweet.followers_count
df.loc[i, "User_Location"] = tweet.user.location
df.loc[i, "User_Verified"] = tweet.user.verified
df.loc[i, "User_Lang"] = tweet.lang
df.loc[i, "User_Status"] = tweet.user.statuses_count
df.loc[i, "User_Method"] = tweet.source
df.loc[i, "Fav_Count"] = tweet.favorite_count
df.loc[i, "RT_Count"] = tweet.retweet_count
df.loc[i, "Tweet_date"] = tweet.created_at
i+=1
return df
我如何将其改编为 Twitter API 库?
我知道它应该适应这样的东西:
for tweet in api.request('search/tweets', {'q':cash})
但它仍然缺少所需的时间跨度。而且我不确定特征的名称是否与该库的名称匹配。
使用 TwitterAPI,您可以通过以下方式提出高级搜索请求:
from TwitterAPI import TwitterAPI
SEARCH_TERM = '#AAPL OR #FB OR #KO OR #ABT OR #PEPCO'
PRODUCT = 'fullarchive'
LABEL = 'your label'
api = TwitterAPI('consumer key', 'consumer secret', 'access token key', 'access token secret')
r = api.request('tweets/search/%s/:%s' % (PRODUCT, LABEL), {'query':SEARCH_TERM})
for item in r:
if 'text' in item:
print(item['text'])
print(item['user']['name'])
print(item['followers_count'])
print(item['user']['location'])
print(item['user']['verified'])
print(item['lang'])
print(item['user']['statuses_count'])
print(item['source'])
print(item['favorite_count'])
print(item['retweet_count'])
print(item['created_at'])
高级搜索 doc 解释了支持的请求参数。做一个日期范围使用这个:
r = api.request('tweets/search/%s/:%s' % (PRODUCT, LABEL),
{'query':SEARCH_TERM, 'fromDate':201501010000, 'toDate':201812310000})
新手程序员在这里寻求帮助。我有一个主题标签列表,我想为其获取从 01-01-2015 到 31-12-2018 的所有历史推文。
我尝试使用 Tweepy 库,但它只允许访问最近 7 天的推文。我还尝试使用 GetOldTweets,因为它可以访问历史推文,但它一直在崩溃。所以现在我已经获得了 Twitter 的高级 API 访问权限,这也让我可以访问完整的历史推文。
为了使用 premium API 进行查询,我不能使用 Tweepy 库(因为它没有 link 和 premium APIs 对吗?)我的选择是 TwitterAPI 和 Search-Tweets.
1- TwitterAPI 和 Search-Tweets 是否提供有关用户名、用户位置、用户是否经过验证、推文语言、推文来源、计数等信息转推和收藏以及每条推文的日期? (就像 tweepy 那样)。我找不到有关此的任何信息。
2- 我可以在查询中提供时间跨度吗?
3- 我该怎么做所有这些?
这是我的 Tweepy 库代码:
hashtags = ["#AAPL","#FB","#KO","#ABT","#PEPCO",...]
df = pd.DataFrame(columns = ["Hashtag", "Tweets", "User", "User_Followers",
"User_Location", "User_Verified", "User_Lang", "User_Status",
"User_Method", "Fav_Count", "RT_Count", "Tweet_date"])
def tweepy_df(df,tags):
for cash in tags:
i = len(df)+1
for tweet in tweepy.Cursor(api.search, q= cash, since = "2015-01-01", until = "2018-12-31").items():
print(i, end = '\r')
df.loc[i, "Hashtag"] = cash
df.loc[i, "Tweets"] = tweet.text
df.loc[i, "User"] = tweet.user.name
df.loc[i, "User_Followers"] = tweet.followers_count
df.loc[i, "User_Location"] = tweet.user.location
df.loc[i, "User_Verified"] = tweet.user.verified
df.loc[i, "User_Lang"] = tweet.lang
df.loc[i, "User_Status"] = tweet.user.statuses_count
df.loc[i, "User_Method"] = tweet.source
df.loc[i, "Fav_Count"] = tweet.favorite_count
df.loc[i, "RT_Count"] = tweet.retweet_count
df.loc[i, "Tweet_date"] = tweet.created_at
i+=1
return df
我如何将其改编为 Twitter API 库?
我知道它应该适应这样的东西:
for tweet in api.request('search/tweets', {'q':cash})
但它仍然缺少所需的时间跨度。而且我不确定特征的名称是否与该库的名称匹配。
使用 TwitterAPI,您可以通过以下方式提出高级搜索请求:
from TwitterAPI import TwitterAPI
SEARCH_TERM = '#AAPL OR #FB OR #KO OR #ABT OR #PEPCO'
PRODUCT = 'fullarchive'
LABEL = 'your label'
api = TwitterAPI('consumer key', 'consumer secret', 'access token key', 'access token secret')
r = api.request('tweets/search/%s/:%s' % (PRODUCT, LABEL), {'query':SEARCH_TERM})
for item in r:
if 'text' in item:
print(item['text'])
print(item['user']['name'])
print(item['followers_count'])
print(item['user']['location'])
print(item['user']['verified'])
print(item['lang'])
print(item['user']['statuses_count'])
print(item['source'])
print(item['favorite_count'])
print(item['retweet_count'])
print(item['created_at'])
高级搜索 doc 解释了支持的请求参数。做一个日期范围使用这个:
r = api.request('tweets/search/%s/:%s' % (PRODUCT, LABEL),
{'query':SEARCH_TERM, 'fromDate':201501010000, 'toDate':201812310000})