How to get users' geolocation in Tweepy?

Viewed 28

Goal: search strings for 'environment' and 'stats' occurring together on Twitter. Draw out results for users' names and their geolocation (where given).

Barrier: places = {p['id']: p for p in response.includes['places']} KeyError: 'places'

CODE:

import tweepy
import config
client = tweepy.Client(bearer_token=config.BEARER_TOKEN)

query = 'environment stats'
response = client.search_recent_tweets(query=query, max_results=100, tweet_fields=['created_at', 'lang'], expansions=['geo.place_id'])

places = {p['id']: p for p in response.includes['places']}

for tweet in response.data:
    if places[tweet.geo['place_id']]:
        place = places[tweet.geo['place_id']]
        print(tweet.id)
        print(place.full_name)
0 Answers
Related