我们如何从特定国家/地区获取推文

use*_*790 11 python twitter country geocoding tweets

我已经阅读了很多关于这部分的内容,我发现的是编写地理编码并搜索推文,例如 https://api.twitter.com/1.1/search/tweets.json?geocode=37.781157,-122.398720,1mi&count = 10

根据我在twitter网站上找到的内容返回位于给定纬度/经度的给定半径内的用户的推文.使用半径修改器时,将考虑最多1,000个不同的"子区域".示例值:37.781157,-122.398720,1mi

问题!,我们如何定义或绘制纬度和经度?我已经尝试了谷歌地图,但我只得到一点,然后我可以在这一点附近添加里程,但这还不够,我希望整个国家被包括在内,这可能吗?

ale*_*cxe 22

一种方法是使用twitter 地理搜索API,获取地点ID,然后使用执行常规搜索place:place_id.例如,使用tweepy:

import tweepy

auth = tweepy.OAuthHandler(..., ...)
auth.set_access_token(..., ...)

api = tweepy.API(auth)
places = api.geo_search(query="USA", granularity="country")
place_id = places[0].id

tweets = api.search(q="place:%s" % place_id)
for tweet in tweets:
    print tweet.text + " | " + tweet.place.name if tweet.place else "Undefined place"
Run Code Online (Sandbox Code Playgroud)

另见这些主题:

UPD(使用python-twitter的相同示例):

from twitter import *


t = Twitter(auth=OAuth(..., ..., ..., ...))

result = t.geo.search(query="USA", granularity="country")
place_id = result['result']['places'][0]['id']

result = t.search.tweets(q="place:%s" % place_id)
for tweet in result['statuses']:
    print tweet['text'] + " | " + tweet['place']['name'] if tweet['place'] else "Undefined place"
Run Code Online (Sandbox Code Playgroud)

希望有所帮助.