简体   繁体   English

如何把cookies转到我要下载html的页面?

[英]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.

相关问题 我如何使用 AJAX 和 Flask 将诸如变量之类的信息从 javascript 传输到 python - How would I transfer information like a variable from javascript to python using AJAX and Flask 从页面抓取 html 但我需要 cookies - Webscraping the html from a page but I need the cookies 我怎样才能像这样提取 cookies? - How can i extract cookies like this? 我如何将数据(变量)从一个 html 页面传输到另一个页面,这里我使用 html 和 django 框架 - How i transfer data(variables) from one html page to another page, here i am using html & django framework Python、django、html:我做了一个预订表格,我想将此页面设置为仅登录访问而不继承“LoginRequiredMixin” - Python, django, html: I made a booking form and I would like to set this page to login access only without inheriting 'LoginRequiredMixin' 我想将 pandas 结果中的 html 插入整个 html - I would like to insert a html from result of pandas into an entire html 如何将CSV“单词”作为字符串传输到Python中 - How would I transfer CSV “words” into Python as strings 我将如何对这样的代码进行单元测试? - How would I unit test code like this? 我想在 Django 中将视图中的数据操作到/渲染(html) - I would like to manipulate the data from the views to / render (html) in Django 我想将原始 HTML 传递给 QWebEngineView 但出现错误 - I would like to pass raw HTML to QWebEngineView but getting errors
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM