Twitter API:如何根据查询词和预定时间跨度和推文特征搜索推文

问题描述 投票:1回答:1

[新手程序员在这里寻求帮助。我有一个主题标签列表,我想要为其获取从2015年1月1日到2018年12月31日的所有历史推文。

我尝试使用Tweepy库,但该库仅允许在最近7天发送推文。我还尝试使用GetOldTweets,因为它可以访问历史推文,但一直崩溃。因此,现在我已经获得了Twitter的高级API访问权限,这也使我可以访问完整的历史推文。

为了使用高级API进行查询,我无法使用Tweepy库(因为它没有与高级API链接,对吗?),我的选择是在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})

但是它仍然缺少期望的时间跨度。而且我不确定这些特性的名称是否与此库的名称匹配。

python api dataframe twitter twitterapi-python
1个回答
1
投票

使用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})
© www.soinside.com 2019 - 2024. All rights reserved.