Python 创建 cookie,然后使用 cookie 加载页面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4349147/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 15:21:27  来源:igfitidea点击:

Python create cookies and then load a page with the cookies

pythoncookiespython-3.xhttplib2

提问by Mac Fly


I would like to access a web page from a python program. I have to set up cookies to load the page.
I used the httplib2 library, but I didn't find how add my own cookie


我想从 python 程序访问网页。我必须设置 cookie 才能加载页面。
我使用了 httplib2 库,但我没有找到如何添加我自己的 cookie

resp_headers, content = h.request("http://www.theURL.com", "GET")

How can I create cookies with the right name and value, add it to the function and then load the page?
Thanks

如何创建具有正确名称和值的 cookie,将其添加到函数然后加载页面?
谢谢

采纳答案by khachik

http = httplib2.Http()
# get cookie_value here
headers = {'Cookie':cookie_value}
response, content = http.request("http://www.theURL.com", 'GET', headers=headers)

You may want to add another header parameters to specify another HTTP request parameters.

您可能希望添加另一个标头参数以指定另一个 HTTP 请求参数。

回答by sultan

From http://code.google.com/p/httplib2/wiki/Exampleshope will help )

来自http://code.google.com/p/httplib2/wiki/Examples希望会有所帮助)

Cookies

饼干

When automating something, you often need to "login" to maintain some sort of session/state with the server. Sometimes this is achieved with form-based authentication and cookies. You post a form to the server, and it responds with a cookie in the incoming HTTP header. You need to pass this cookie back to the server in subsequent requests to maintain state or to keep a session alive.

在自动化某些事情时,您通常需要“登录”以维护与服务器的某种会话/状态。有时这是通过基于表单的身份验证和 cookie 来实现的。您将表单发布到服务器,它会在传入的 HTTP 标头中使用 cookie 进行响应。您需要在后续请求中将此 cookie 传递回服务器以保持状态或保持会话活动。

Here is an example of how to deal with cookies when doing your HTTP Post.

这是在执行 HTTP Post 时如何处理 cookie 的示例。

First, lets import the modules we will use:

首先,让我们导入我们将使用的模块:

import urllib
import httplib2

Now, lets define the data we will need. In this case, we are doing a form post with 2 fields representing a username and a password.

现在,让我们定义我们需要的数据。在这种情况下,我们正在做一个包含 2 个字段代表用户名和密码的表单发布。

url = 'http://www.example.com/login'   
body = {'USERNAME': 'foo', 'PASSWORD': 'bar'}
headers = {'Content-type': 'application/x-www-form-urlencoded'}

Now we can send the HTTP request:

现在我们可以发送 HTTP 请求:

http = httplib2.Http()
response, content = http.request(url, 'POST', headers=headers, body=urllib.urlencode(body))

At this point, our "response" variable contains a dictionary of HTTP header fields that were returned by the server. If a cookie was returned, you would see a "set-cookie" field containing the cookie value. We want to take this value and put it into the outgoing HTTP header for our subsequent requests:

此时,我们的“响应”变量包含服务器返回的 HTTP 标头字段的字典。如果返回了 cookie,您将看到一个包含 cookie 值的“set-cookie”字段。我们想要获取这个值并将其放入我们后续请求的传出 HTTP 标头中:

headers['Cookie'] = response['set-cookie']

Now we can send a request using this header and it will contain the cookie, so the server can recognize us.

现在我们可以使用这个标头发送一个请求,它会包含 cookie,所以服务器可以识别我们。

So... here is the whole thing in a script. We login to a site and then make another request using the cookie we received:

所以......这是脚本中的全部内容。我们登录到一个站点,然后使用我们收到的 cookie 发出另一个请求:

#!/usr/bin/env python

import urllib
import httplib2

http = httplib2.Http()

url = 'http://www.example.com/login'   
body = {'USERNAME': 'foo', 'PASSWORD': 'bar'}
headers = {'Content-type': 'application/x-www-form-urlencoded'}
response, content = http.request(url, 'POST', headers=headers, body=urllib.urlencode(body))

headers = {'Cookie': response['set-cookie']}

url = 'http://www.example.com/home'   
response, content = http.request(url, 'GET', headers=headers)

回答by Alberto Perez

You can also use just urllib2library

你也可以只使用urllib2

        import urllib2

        opener = urllib2.build_opener()
        opener.addheaders.append(('Cookie', 'cookie1=value1;cookie2=value2'))
        f = opener.open("http://www.example.com/")
        the_page_html = f.read()