如何解决Tweepy API找不到英国
试图使用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")
解决方法
您使用的 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,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']
在使用之前,您应该检查列表中每个地方的名称,以确定与英国对应的名称。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。