[英]How to transfer cookies to the page that I would like to download html?
Executes the code:执行代码:
response = requests.get("https://google.com")
print(response.cookies)
cookies_dict = {"my_cookie": "cookie_value"}
response = requests.get("https://google.com", cookies=cookies_dict)
print(response.cookies)
I get a reply:我得到回复:
<RequestsCookieJar[]>
<RequestsCookieJar[]>
I use libraries:我使用图书馆:
from urllib.request import Request
I would like to transfer the cookie data to the website so that it can download the HTML content from the website with the use of this cookie.我想将 cookie 数据传输到网站,以便它可以使用此 cookie 从网站下载 HTML 内容。 Unfortunately, when you try to send cookies, they are not added.不幸的是,当您尝试发送 cookies 时,它们没有被添加。 I have already tried many ways.我已经尝试了很多方法。 Unsuccessfully.不成功。
Could someone help me how to send cookies?有人可以帮我怎么发cookies吗? How to otherwise download the html content of a page with assigned cookies?如何以其他方式下载分配了 cookies 的页面的 html 内容?
The website I use in the project uses cookies for authorization.我在项目中使用的网站使用cookies进行授权。 Without sending the cookie, I will not be able to download the content of the website.如果不发送 cookie,我将无法下载网站内容。
//edit I changed the page example because it did not work like my project needed page. //edit 我更改了页面示例,因为它不像我的项目需要的页面那样工作。
If you print response.text, you can see the cookies如果你打印 response.text,你可以看到 cookies
import requests
response = requests.get("http://httpbin.org/cookies")
print(response.text)
cookies_dict = {"my_cookie": "cookie_value"}
response = requests.get("http://httpbin.org/cookies", cookies=cookies_dict)
print(response.text)
Output Output
{
"cookies": {}
}
{
"cookies": {
"my_cookie": "cookie_value"
}
}
Or you can access the cookies directly by using response.json()['cookies']
或者您可以使用response.json()['cookies']
直接访问 cookies
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.