python - 推特 API : How to search tweets based on query words and predetermined time span + tweets characteristics

标签 python api dataframe twitter twitterapi-python

新手程序员在这里寻求帮助。我有一个主题标签列表,我想获取从 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})

但它仍然缺少所需的时间跨度。我不确定特征的名称是否与该库的名称匹配。

最佳答案

使用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})

关于python - 推特 API : How to search tweets based on query words and predetermined time span + tweets characteristics,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58410167/

相关文章:

python - 创建缓存以更快地访问python中的字典列表

python - python中用户定义的图例

python - 如何将用户输入字符串作为 python 脚本中的参数传递给函数?

c# - 返回数据时出现 WCF CommunicationException

php - API连接到mysql有时不执行查询

php - 使用 mailchimp api 发送电子邮件

r - 使用 dplyr 更改 R 数据帧中最后一列最后一行的值

r - 按每行中 NA 的数量对数据进行排序

python - 让 Python 装饰器与 Hug API 框架配合使用

r - 识别并标记 r 中的重复行