14

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?

4

1 回答 1

28

一种方法是使用 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"

另请参阅这些线程:

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"

希望有帮助。

于 2013-07-13T20:01:49.113 回答