How to create a pandas dataframe using Tweepy?

前端 未结 2 1340
梦谈多话
梦谈多话 2021-01-01 01:19

In Python 3 I made program to extract posts and likes in Twitter:

import tweepy
import pandas as pd

consumer_key = \'\'
consumer_secret = \'\'
access_token          


        
2条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-01 02:01

    Here's an easy way:

    import os
    import tweepy
    import pandas as pd
    
    # use os.environ.get to obtain other environment variables
    # from ~/.bashrc or ~/.zshrc etc., so they aren't in your code
    consumer_key = os.environ.get('c_key')
    consumer_secret = # os...
    access_token = # os...
    access_token_secret = # os...
    auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
    auth.set_access_token(access_token, access_token_secret)
    api = tweepy.API(auth)
    
    results = api.search(q='cheese', count=100)
    
    json_data = [r._json for r in results]
    
    df = pd.io.json.json_normalize(json_data)
    

提交回复
热议问题