在python中搜索给定时间窗口中的tweet

2024-04-26 12:50:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我希望所有2013年5月到2014年5月的微博都包含一个给定的词。在

我查看了GET search/tweets的API文档,但它似乎不允许您给出时间窗口,只允许您提供日期,它将检索7天前的tweet。在

如何在python中检索这些tweet?(基本上,我想写一个脚本来完成 Twitter advanced search的功能


Tags: 文档功能脚本apisearchget时间twitter
2条回答

您将不得不将您的Twitter提要转储到JSON并解析它以获得所需的tweets。我只是在Python中使用tweepy和json模块将其组合在一起。在

#!/usr/bin/env python

import tweepy
from tweepy import OAuthHandler
import json


def process_or_store(tweet):
    converted = json.dumps(tweet)
    parsed = json.loads(converted)
    return parsed


access_token = ''
access_secret = ''
consumer_key = ''
consumer_secret = ''
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_secret)
api = tweepy.API(auth)

for tweet in tweepy.Cursor(api.user_timeline).items():
    j = process_or_store(tweet._json)
    m2013 = ['May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec', '2013']
    m2014 = ['Jan', 'Feb', 'Mar', 'Apr', 'May', '2014']

    if all(x in j['created_at'] for x in m2013):
        print "%s   %s" % (j['created_at'], j['text'])
    elif all(x in j['created_at'] for x in m2014):
        print "%s   %s" % (j['created_at'], j['text'])

根据Twitter搜索API文档,您想要的查询是不可能的:https://dev.twitter.com/rest/public/search

The Twitter Search API searches against a sampling of recent Tweets published in the past 7 days.

在过去的7天里,你想要达到的目标只能通过在Twitter上手动搜索一个帐户来实现。在

你可以试试twarc 对于你提到的高级搜索运营商,但我不确定它是否会根据Twitter搜索API文档查询一整年。在

虽然不是基于Python的,但是一个替代方法是使用https://webrecorder.io/

滚动到要录制或尝试捕获整个源的时间。请注意自动滚动选项。在

相关问题 更多 >