首页 > 解决方案 > Tweepy 地理搜索不断返回超出范围的推文

问题描述

请帮忙!我正在尝试使用免费的 tweepy API 完成特定于地理的查询搜索。我意识到返回的推文坐标/地点仍然来自全球各地。即使我简化代码进行调查,geo 仍然无法工作:

# Get auth - deleter is a function I made to format .txt into string, note the appauth not oauth.

def startup(loc):
    consumer_secret = deleter(open(loc[0],'r').read(),'\n')
    consumer_key = deleter(open(loc[1],'r').read(),'\n')
    auth = tweepy.AppAuthHandler(consumer_key, consumer_secret)
    api = tweepy.API(auth, wait_on_rate_limit=True,
                   wait_on_rate_limit_notify=True)
    if (not api):
        print ("Can't Authenticate"),sys.exit(-1)
    return api

# Keys
loc = ['consumer_scret.txt', 'api.txt']
api = startup(loc)

# Search parameters
query = "lockdown"
gg = "5.29126,52.132633,10km"


results = api.search(q=query,geo=gg, count = 100)
for tweet in results:
   print(tweet.id,tweet.geo,tweet.place,tweet.coordinates)

任何帮助将非常感激!

标签: pythonapigeolocationtweepytweets

解决方案


我认为您的地理参数被忽略了。传递给搜索 api 的参数名称是geocode,而不是geo。尝试这个:

results = api.search(q=query, geocode=gg, count=100)

当我以小至 10 公里的搜索半径运行它时,我没有得到任何结果。如果我将半径扩大到 1000 公里,我会得到更多。

参考: API.search


推荐阅读