How to perform oauth when doing twitter scraping with python requests

后端 未结 1 812
半阙折子戏
半阙折子戏 2021-01-18 09:35

I am trying to retrieve 100 recent tweets of a user. It is working well with tweepy module in Python. But how can I do the same with requests in python. I want to do:

<
相关标签:
1条回答
  • 2021-01-18 10:19

    You can use requests-oauthlib as described in requests docs.

    OAuth:

    import requests
    from requests_oauthlib import OAuth1
    url = 'https://api.twitter.com/1.1/account/verify_credentials.json'
    auth = OAuth1(API_KEY, API_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
    requests.get(url, auth=auth)
    

    Get tweets:

    r = requests.get('https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=stackoverflow&count=100', auth=auth)
    for tweet in r.json():
      print tweet['text']
    
    0 讨论(0)
提交回复
热议问题