使用 Python 请求:会话、Cookie 和 POST [英] Using Python Requests: Sessions, Cookies, and POST

查看:61
本文介绍了使用 Python 请求:会话、Cookie 和 POST的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 StubHub API 抓取一些销售数据.此处显示的数据示例:

I am trying to scrape some selling data using the StubHub API. An example of this data seen here:

https://sell.stubhub.com/sellapi/event/4236070/section/null/seatmapdata

您会注意到,如果您在未登录 stubhub.com 的情况下尝试访问该网址,它将无法正常工作.您需要先登录.

You'll notice that if you try and visit that url without logging into stubhub.com, it won't work. You will need to login first.

通过网络浏览器登录后,我在新选项卡中打开要抓取的 URL,然后使用以下命令检索抓取的数据:

Once I've signed in via my web browser, I open the URL which I want to scrape in a new tab, then use the following command to retrieve the scraped data:

r = requests.get('https://sell.stubhub.com/sellapi/event/4236070/section/null/seatmapdata')

但是,一旦浏览器会话在十分钟后过期,我就会收到此错误:

However, once the browser session expires after ten minutes, I get this error:

<FormErrors>
<FormField>User Auth Check</FormField>
<ErrorMessage>
Either is not active or the session might have expired. Please login again.
</ErrorMessage>

我认为我需要通过 cookie 实现会话 ID 以保持我的身份验证有效.

I think that I need to implement the session ID via cookie to keep my authentication alive and well.

对于以前从未做过此类事情的人来说,Requests 库文档非常糟糕,所以我希望你们能提供帮助.

The Requests library documentation is pretty terrible for someone who has never done this sort of thing before, so I was hoping you folks might be able to help.

Requests 提供的例子是:

The example provided by Requests is:

s = requests.Session()

s.get('http://httpbin.org/cookies/set/sessioncookie/123456789')
r = s.get("http://httpbin.org/cookies")

print r.text
# '{"cookies": {"sessioncookie": "123456789"}}'

老实说,我无法对此做出正面或反面.如何在 POST 请求之间保留 Cookie?

I honestly can't make heads or tails of that. How do I preserve cookies between POST requests?

推荐答案

我不知道 stubhub 的 api 是如何工作的,但一般应该是这样的:

I don't know how stubhub's api works, but generally it should look like this:

s = requests.Session()
data = {"login":"my_login", "password":"my_password"}
url = "http://example.net/login"
r = s.post(url, data=data)

现在您的会话包含登录表单提供的 cookie.要访问此会话的 cookie,只需使用

Now your session contains cookies provided by login form. To access cookies of this session simply use

s.cookies

任何进一步的操作,比如另一个请求都会有这个 cookie

Any further actions like another requests will have this cookie

这篇关于使用 Python 请求:会话、Cookie 和 POST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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