Scrape User Location from Twitter

前端 未结 1 1645
-上瘾入骨i
-上瘾入骨i 2021-01-12 08:06

I am trying to scrape latitude and longitude of user from Twitter with respect to user names. The user name list is a csv file with more than 50 names in one input file. Th

相关标签:
1条回答
  • 2021-01-12 08:17

    Assuming that you just want to get the location of the user that is put up in his/her profile page, you can just use the API.get_user from Tweepy. Below is the working code.

    #!/usr/bin/env python
    from __future__ import print_function
    
    #Import the necessary methods from tweepy library
    import tweepy
    from tweepy import OAuthHandler
    
    
    #user credentials to access Twitter API 
    access_token = "your access token here"
    access_token_secret = "your access token secret key here"
    consumer_key = "your consumer key here"
    consumer_secret = "your consumer secret key here"
    
    
    def get_user_details(username):
            userobj = api.get_user(username)
            return userobj
    
    
    if __name__ == '__main__':
        #authenticating the app (https://apps.twitter.com/)
        auth = tweepy.auth.OAuthHandler(consumer_key, consumer_secret)
        auth.set_access_token(access_token, access_token_secret)
        api = tweepy.API(auth)
    
        #for list of usernames, put them in iterable and call the function
        username = 'thinkgeek'
        userOBJ = get_user_details(username)
        print(userOBJ.location)
    

    Note: This is a crude implementation. Write a proper sleeper function to obey Twitter API access limits.

    0 讨论(0)
提交回复
热议问题