Open In App

Python – API.geo_id() in Tweepy

Last Updated : 11 Jun, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

Twitter is a popular social network where users share messages called tweets. Twitter allows us to mine the data of any user using Twitter API or Tweepy. The data will be tweets extracted from the user. The first thing to do is get the consumer key, consumer secret, access key and access secret from twitter developer available easily for each user. These keys will help the API for authentication.

API.geo_id()

The geo_id() method of the API class in Tweepy module is used to fetch a location by its Twitter ID.

Syntax : API.geo_id(id)

Parameters :

  • id : Twitter ID of the location.

Returns : a list of objects of class Place

Example 1 :




# import the module
import tweepy
  
# assign the values accordingly
consumer_key = ""
consumer_secret = ""
access_token = ""
access_token_secret = ""
  
# authorization of consumer key and consumer secret
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
  
# set access to user's access key and access secret 
auth.set_access_token(access_token, access_token_secret)
  
# calling the api 
api = tweepy.API(auth)
  
# Twitter ID of London
id = "457b4814b4240d87"
  
# fetching the location
location = api.geo_id(id)
  
# printing the information
print("Place Type : " + location.place_type)
print("Name : " + location.name)
print("Full Name : " + location.full_name)
print("Country : " + location.country)


Output :

Place Type : city
Name : London
Full Name : London, England
Country : United Kingdom

Example 2 : Exception is raised when giving invalid ID.




# invalid ID
id = "xyz"
  
# fetching the location
location = api.geo_id(id)


Output :

    raise TweepError(error_msg, resp, api_code=api_error_code)
tweepy.error.TweepError: [{'message': 'Sorry, that page does not exist', 'code': 34}]


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads