python - 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")
解决方案
您使用的 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']
在使用之前,您应该检查列表中每个地方的名称,以确定与英国相对应的地方。
推荐阅读
- java - 无法将字符串解析为 Java 8 LocalDateTime
- c# - C# Automapper 内部列表来自另一个不同类型的内部列表
- git - 将代码从 repoA 的一个分支移动到另一个存储库 repoB
- ios - 如何在 swift 上将元素添加到新的 UIViewController?
- javascript - 如何通过循环获取数组元素的索引
- java - 有没有更好的替代方法来代替 SharedPreferences 来保存/缓存大量数据而无需在线访问?
- c# - 查找字符串中包含多少次 Anagram - asp.net c#
- c# - 从查询字符串c#中读取的通用函数
- python - Django Rest Framework 在 PUT 和 PATCH 上返回 302 并且不更新任何内容
- android - react-native apk 在 android 设备上不起作用