我正在尝试使用Twitter REST API搜索特定的推文。我只想检索在伦敦市中启用了位置并且已启用坐标的推文。但是,我不确定如何在查询中实际编写此代码。
此刻,我所拥有的代码搜索在文本框中输入的单个关键字,并返回包含给定单词的推文。例如:
searchW = "'"+keyword+"'"
maxTweets = 500
tweetsPerQry = 100
tweets = api.search(q=searchW, count=tweetsPerQry)
无论如何,我是否可以扩展该查询以包括我提到的过滤器?我已经阅读了文档,但是不确定如何组织它。
答案 0 :(得分:0)
首先,您必须获取伦敦中心的纬度和经度。 您可以从openstreetmap或googlemap获取信息(类似于51.5128,-0.1171)。
请参考https://developer.twitter.com/en/docs/tweets/search/api-reference/get-search-tweets.html的文档,您必须使用geocode
参数,并以英里或公里为单位添加半径。
因此要获取包含您的关键字并在2公里半径内位于伦敦的推文:
searchW = "'"+keyword+"'"
maxTweets = 500
tweetsPerQry = 100
tweets = api.search(q=searchW, count=tweetsPerQry, geocode="51.5128,-0.1171,2km")
for tweet in tweets:
if tweet.coordinates is not None:
lon = tweet.coordinates['coordinates'][0]
lat = tweet.coordinates['coordinates'][1]
print('lat', lat, 'lon', lon)
print(tweet.user.id_str, tweet.user.screen_name, tweet.text, tweet.coordinates)