我们如何从特定国家/地区获取推文 [英] How can we get tweets from specific country
问题描述
我已经阅读了很多关于这部分的内容,我发现编写地理编码并搜索推文例如https://api.twitter.com/1.1/search/tweets.json?geocode=37.781157,-122.398720,1mi&count=10
I've read a lot about this part and what I found is to write the geocode and search for tweets for example https://api.twitter.com/1.1/search/tweets.json?geocode=37.781157,-122.398720,1mi&count=10
根据我在推特网站上找到的返回位于给定纬度/经度的给定半径内的用户的推文.使用半径修改器时将考虑最多 1,000 个不同的子区域".示例值:37.781157,-122.398720,1mi
according to what i found in twitter website Returns tweets by users located within a given radius of the given latitude/longitude. A maximum of 1,000 distinct "sub-regions" will be considered when using the radius modifier. Example Values: 37.781157,-122.398720,1mi
问题!,我们如何定义或绘制纬度和经度?我试过谷歌地图,但我只得到一个点,然后我可以在这个点周围添加英里数,但这还不够,我想包括整个国家,这可能吗?
The question!, how can we define or draw the latitude and longitude ? I've tried google map but I only get a point then i can add the miles around this point, but this is not enough, I want the whole country to be included, is that possible?
推荐答案
一种方法是使用 twitter 地理搜索 API,获取地点 id,然后使用 place:place_id
执行常规搜索.例如,使用 tweepy:
One way is to use twitter geo search API, get the place id and then perform regular search using place:place_id
. Example, using 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"
另见这些主题:
UPD(使用 python-twitter 的相同示例):
UPD (the same example using 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"
希望有所帮助.
这篇关于我们如何从特定国家/地区获取推文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!