Sunday, October 5, 2025
HomeLanguagesPython – API.geo_id() in Tweepy

Python – API.geo_id() in Tweepy

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}]
Last Updated :
11 Jun, 2020
Like Article
Save Article

<!–

–>

Similar Reads
Related Tutorials
Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32337 POSTS0 COMMENTS
Milvus
86 POSTS0 COMMENTS
Nango Kala
6706 POSTS0 COMMENTS
Nicole Veronica
11871 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11936 POSTS0 COMMENTS
Shaida Kate Naidoo
6823 POSTS0 COMMENTS
Ted Musemwa
7089 POSTS0 COMMENTS
Thapelo Manthata
6779 POSTS0 COMMENTS
Umr Jansen
6779 POSTS0 COMMENTS