Tweepy 空白搜索

4 投票
1 回答
2927 浏览
提问于 2025-04-17 16:15

我在网上找答案找了很久,但没找到我认为可能是个简单问题的解答:

有没有办法用tweepy获取“空白”搜索结果?

举个例子,想要获取某个地点的所有推文。

我不能发送一个没有查询内容的请求:

query=  tweepy.api.search(q="", rpp=1000)

结果是:

tweepy.error.TweepError: 你必须输入一个查询。

我想要的是这样的查询,比如:

http://search.twitter.com/search.atom?geocode=38.376,-0.5,8km

获取所有来自……的推文。

1 个回答

7

q 参数是可选的,geocode 也是可选的(具体可以查看 文档)。

这个对我来说有效:

import tweepy

auth = tweepy.OAuthHandler(<consumer_key>, <consumer_secret>)
auth.set_access_token(<key>, <secret>)

api = tweepy.API(auth)

results = tweepy.api.search(geocode="38.376,-0.5,8km", rpp=1000)

for result in results:
    print result.text
    print result.location if hasattr(result, 'location') else "Undefined location"

希望这对你有帮助。

撰写回答