首页 > 解决方案 > Tweepy 找不到英国

问题描述

我正在尝试使用 Tweepy 获取英国的推文。但是,我只会得到阿拉伯语?

tweets = tw.Cursor(api.search,
                   q=search_term,
                   lang="en",
                   since='2019-11-01').items(1000)
api2 = tweepy.API(auth)
places = api.geo_search(query="United Kingdom", granularity="country")
place_id = places[0].id
tweets = api2.search(q="place:%s" % place_id)
for tweet in tweets:
    print (tweet.text + " | " + tweet.place.name if tweet.place else "Undefined place")

标签: pythontweepy

解决方案


您使用的 ID 可能是“约旦哈希姆王国”:

>>> places = api.geo_search(query="United Kingdom", granularity="country")
>>> places[0]
Place(_api=<tweepy.api.API object at 0x00000201E04358B0>, id='fc6282dff859b848', name='Hashemite Kingdom of Jordan', full_name='Hashemite Kingdom of Jordan', country='Hashemite Kingdom of Jordan', country_code='JO', url='https://api.twitter.com/1.1/geo/id/fc6282dff859b848.json', place_type='country', attributes={}, bounding_box=BoundingBox(_api=<tweepy.api.API object at 0x00000201E04358B0>, type='Polygon', coordinates=[[[34.9427626, 29.1862444], [34.9427626, 33.3748822], [39.3011038, 33.3748822], [39.3011038, 29.1862444], [34.9427626, 29.1862444]]]), centroid=[36.31971222488649, 31.2805633], contained_within=[])

您可以看到英国实际上是结果之一:

>>> [place.name for place in places]
['Hashemite Kingdom of Jordan', 'United Kingdom', 'United States', 'Kingdom of Saudi Arabia', 'United Arab Emirates', 'United States Minor Outlying Islands']

在使用之前,您应该检查列表中每个地方的名称,以确定与英国相对应的地方。


推荐阅读