Parsing Twitter JSON object in Python

前端 未结 4 1296
一生所求
一生所求 2021-01-07 04:39

I am trying to download tweets from twitter.

I have used python and Tweepy for this. Though I am new to both Python and Twitter API.

My Python script is as f

4条回答
  •  余生分开走
    2021-01-07 05:23

    Tweepy gives you richer objects; it parsed the JSON for you.

    The SearchResult objects have the same attributes as the JSON structures that Twitter sent; just look up the Tweet documentation to see what is available:

    for result in api.search(q="football"):
        print result.text
    

    Demo:

    >>> import tweepy
    >>> tweepy.__version__
    '3.3.0'
    >>> consumer_key = ''
    >>> consumer_secret = ''
    >>> access_token = ''
    >>> access_token_secret = ''
    >>> auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
    >>> auth.set_access_token(access_token, access_token_secret)
    >>> api = tweepy.API(auth)
    >>> for result in api.search(q="football"):
    ...     print result.text
    ... 
    Great moments from the Women's FA Cup http://t.co/Y4C0LFJed9
    RT @freebets: 6 YEARS AGO TODAY: 
    
    Football lost one of its great managers. 
    
    RIP Sir Bobby Robson. http://t.co/NCo90ZIUPY
    RT @Oddschanger: COMPETITION CLOSES TODAY!
    
    Win a Premier League or Football League shirt of YOUR choice! 
    
    RETWEET & FOLLOW to enter. http…
    Berita Transfer: Transfer rumours and paper review – Friday, July 31 http://t.co/qRrDIEP2zh [TS] #nobar #gosip
    @ajperry18 im sorry I don't know this football shit

提交回复
热议问题