Managing Tweepy API Search

后端 未结 5 2029
情书的邮戳
情书的邮戳 2020-11-30 20:25

Please forgive me if this is a gross repeat of a question previously answered elsewhere, but I am lost on how to use the tweepy API search function. Is there any documentati

5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-30 20:59

    I am working on extracting twitter data for around a location( in here, around India), for all tweets which include a special keyword or a list of keywords.

    import tweepy
    import credentials    ## all my twitter API credentials are in this file, this should be in the same directory as is this script
    
    ## set API connection
    auth = tweepy.OAuthHandler(credentials.consumer_key, 
                                credentials.consumer_secret)
    auth.set_access_secret(credentials.access_token, 
                            credentials.access_secret)
        
    api = tweepy.API(auth, wait_on_rate_limit=True)    # set wait_on_rate_limit =True; as twitter may block you from querying if it finds you exceeding some limits
    
    search_words = ["#covid19", "2020", "lockdown"]
    
    date_since = "2020-05-21"
    
    tweets = tweepy.Cursor(api.search, =search_words,
                           geocode="20.5937,78.9629,3000km",
                           lang="en", since=date_since).items(10)
    ## the geocode is for India; format for geocode="lattitude,longitude,radius"
    ## radius should be in miles or km
    
    
    for tweet in tweets:
        print("created_at: {}\nuser: {}\ntweet text: {}\ngeo_location: {}".
                format(tweet.created_at, tweet.user.screen_name, tweet.text, tweet.user.location))
        print("\n")
    ## tweet.user.location will give you the general location of the user and not the particular location for the tweet itself, as it turns out, most of the users do not share the exact location of the tweet
    

    RESULTS ---- created_at: 2020-05-28 16:48:23 user: XXXXXXXXX tweet text: RT @Eatala_Rajender: Media Bulletin on status of positive cases #COVID19 in Telangana. (Dated. 28.05.2020)

    TelanganaFightsCorona

    StayHom…

    geo_location: Hyderabad, India

提交回复
热议问题