5

I am starting to make a python program to get user locations, I've never worked with the twitter API and I've looked at the documentation but I don't understand much. I'm using tweepy, can anyone tell me how I can do this? I've got the basics down, I found a project on github on how to download a user's tweets and I understand most of it.

Luispe Reyes
  • 81
  • 1
  • 2
  • 6

3 Answers3

3

Once you have the twitter (JSON) search results, you can extract the location information, if it's available. Not all twitter users have included their location information.

for tweet in results:
    print(tweet.get('user', {}).get('location', {}))

After analyzing the twitter API responses for various queries, I think getting the tweet location is not possible since the API does not include it in the JSON response.

kmario23
  • 57,311
  • 13
  • 161
  • 150
  • Can I get some help with a similar kind of problem http://stackoverflow.com/questions/38039842/scrape-user-location-from-twitter – Sitz Blogz Jun 29 '16 at 21:10
2

Once you have a tweet, the tweet includes a user, which belongs to the user model. To call the location just do the following

tweet.user.location

0

I don't believe that Tweepy can get user location data; have you had a look the documentation?

You may be better off with Twython which can call the get statuses/user_timeline API call?

Also bear in mind that for privacy reasons twitter won't give you a lat/long for users; the user has their location made less accurate, usually referencing their "neighborhood".

Ari Cooper-Davis
  • 3,374
  • 3
  • 26
  • 43
  • how about their tweet's location? I think I may have written down my question incorrectly. since I know that you can enable location for tweets is it possible to get it through tweedy? @Ari Cooper-Davis – Luispe Reyes Apr 08 '16 at 02:25
  • It is possible to get location information from users tweets, yes. This location data won't (mostly) be specific enough to plot on a map, but it may be a neighborhood like [Charlton, Manchester](https://www.google.co.uk/maps/search/Charlton,+Manchester/@53.4788483,-2.3299807,12z/). Tweedy is not able to access this location data (as far as I know), but it can be accessed using [Twython](https://twython.readthedocs.org/en/latest/api.html#twython.Twython.get_user_timeline). Does that help? – Ari Cooper-Davis Apr 08 '16 at 02:44
  • I could be wrong actually, try making a [user_timeline](http://tweepy.readthedocs.org/en/v3.5.0/api.html#API.user_timeline) API call too see what is returned by the status objects. – Ari Cooper-Davis Apr 08 '16 at 02:50
  • I will try that! Thank you – Luispe Reyes Apr 08 '16 at 02:51
  • Check out Twitter's (not Tweepy's) documentation. The fields you are probably interested in are `coordinates` and `place`. – Jonas Apr 08 '16 at 03:22