How to perform oauth when doing twitter scraping with python requests

血红的双手。 提交于 2019-12-04 03:20:40

问题


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:

import requests
r = requests.get('https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=xxxx&count=100')

Here how to do the authentication with client key, client secret, access token and access secret, before sending this request?


回答1:


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']


来源:https://stackoverflow.com/questions/33308634/how-to-perform-oauth-when-doing-twitter-scraping-with-python-requests

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!