【问题标题】:Sending http requests when the cookies are changing time to time当 cookie 不时更改时发送 http 请求
【发布时间】:2020-09-21 12:07:10
【问题描述】:

我在使用 python 发送 http 请求时遇到了一些麻烦, 因为 http 标头中的 cookie 会不时自动更改。

如何在不更改 cookie 的情况下通过 http 请求捕获软件发送 http 请求?

我通常会这样发送请求:

import requests as req
url = "somesite.com"
headers = {"user-agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)"}
resp = req.get(url , headers=headers)
print(resp.text)

【问题讨论】:

  • 尝试使用requests.Session
  • @Tarique 你能解释一下吗?

标签: python-3.x cookies python-requests


【解决方案1】:

基本上你需要requests.Session,它会自动处理cookies。

这里有一个示例代码可以尝试:

from requests import Session

# create session object
session = Session()
session.headers['User-Agent'] = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36'


# do requests now
url = "somesite.com"
resp = session.get(url)

请查看文档:https://requests.readthedocs.io/en/latest/user/advanced/

【讨论】:

    猜你喜欢
    • 2013-06-08
    • 2020-08-19
    • 1970-01-01
    • 2014-07-06
    • 2011-05-18
    • 1970-01-01
    • 2015-02-11
    • 2013-01-25
    • 1970-01-01
    相关资源
    最近更新 更多