gpt4 book ai didi

python - 如何使用 Python 登录网页并检索 cookie 以供以后使用?

转载 作者:IT老高 更新时间:2023-10-28 12:26:33 24 4
gpt4 key购买 nike

我想使用 python 下载和解析网页,但要访问它,我需要设置几个 cookie。因此,我需要先通过 https 登录网页。登录时刻包括向/login.php 发送两个 POST 参数(用户名、密码)。在登录请求期间,我想从响应 header 中检索 cookie 并存储它们,以便我可以在请求中使用它们来下载网页/data.php。

我将如何在 python 中执行此操作(最好是 2.6)?如果可能的话,我只想使用内置模块。

最佳答案

这是一个使用优秀的 requests 的版本图书馆:

from requests import session

payload = {
'action': 'login',
'username': USERNAME,
'password': PASSWORD
}

with session() as c:
c.post('http://example.com/login.php', data=payload)
response = c.get('http://example.com/protected_page.php')
print(response.headers)
print(response.text)

关于python - 如何使用 Python 登录网页并检索 cookie 以供以后使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/189555/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com