Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
514 views
in Technique[技术] by (71.8m points)

python - How can we get tweets from specific country

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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"

Also see these threads:

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"

Hope that helps.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...