在使用 python 请求进行 twitter 抓取时如何执行 oauth [英] How to perform oauth when doing twitter scraping with python requests

查看:30
本文介绍了在使用 python 请求进行 twitter 抓取时如何执行 oauth的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试检索用户最近的 100 条推文.它与 Python 中的 tweepy 模块配合良好.但是我如何对 python 中的请求做同样的事情.我想做:

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?

推荐答案

您可以使用 requests-oauthlib 如请求 docs 中所述.

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)

获取推文:

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

这篇关于在使用 python 请求进行 twitter 抓取时如何执行 oauth的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆