未使用python请求模块获取所有cookie信息

未使用python请求模块获取所有cookie信息,python,cookies,python-requests,Python,Cookies,Python Requests,我正在学习如何使用python请求模块登录示例网站。这 让我开始了。从GoogleChrome>Inspect Element>NetworkTab中看到的所有cookie中,我无法使用以下代码检索所有cookie: import requests with requests.Session() as s: url = 'http://www.noobmovies.com/accounts/login/?next=/' s.get(url) allcookies = s.

我正在学习如何使用python请求模块登录示例网站。这 让我开始了。从GoogleChrome>Inspect Element>NetworkTab中看到的所有cookie中,我无法使用以下代码检索所有cookie:

import requests
with requests.Session() as s:
    url = 'http://www.noobmovies.com/accounts/login/?next=/'
    s.get(url)
    allcookies = s.cookies.get_dict()
    print allcookies
使用此选项,我只能获得如下所示的csrftoken:

{'csrftoken': 'ePE8zGxV4yHJ5j1NoGbXnhLK1FQ4jwqO'}
但在google chrome中,除了csrftoken(sessionid、_-gat、_-ga等),我看到了所有其他cookie:

我甚至从中尝试了以下代码,但结果是一样的:

from urllib2 import Request, build_opener, HTTPCookieProcessor, HTTPHandler
import cookielib

#Create a CookieJar object to hold the cookies
cj = cookielib.CookieJar()
#Create an opener to open pages using the http protocol and to process cookies.
opener = build_opener(HTTPCookieProcessor(cj), HTTPHandler())

#create a request object to be used to get the page.
req = Request("http://www.noobmovies.com/accounts/login/?next=/")
f = opener.open(req)

#see the first few lines of the page
html = f.read()
print html[:50]

#Check out the cookies
print "the cookies are: "
for cookie in cj:
    print cookie
输出:

<!DOCTYPE html>
<html xmlns="http://www.w3.org
the cookies are: 
<Cookie csrftoken=ePE8zGxV4yHJ5j1NoGbXnhLK1FQ4jwqO for www.noobmovies.com/>


正在设置的cookie来自其他页面/资源,可能由JavaScript代码加载。您可以使用、或等工具检查它,仅向页面发出请求(不运行JS代码)

此服务器设置的唯一cookie是
csrftoken
,如您所见:

$ wget --server-response 'http://www.noobmovies.com/accounts/login/?next=/'
--2016-02-01 22:51:55--  http://www.noobmovies.com/accounts/login/?next=/
Resolving www.noobmovies.com (www.noobmovies.com)... 69.164.217.90
Connecting to www.noobmovies.com (www.noobmovies.com)|69.164.217.90|:80... connected.
HTTP request sent, awaiting response... 
  HTTP/1.1 200 OK
  Server: nginx/1.4.6 (Ubuntu)
  Date: Tue, 02 Feb 2016 00:51:58 GMT
  Content-Type: text/html; charset=utf-8
  Transfer-Encoding: chunked
  Connection: keep-alive
  Vary: Accept-Encoding
  Expires: Tue, 02 Feb 2016 00:51:58 GMT
  Vary: Cookie,Accept-Encoding
  Cache-Control: max-age=0
  Set-Cookie: csrftoken=XJ07sWhMpT1hqv4K96lXkyDWAYIFt1W5; expires=Tue, 31-Jan-2017 00:51:58 GMT; Max-Age=31449600; Path=/
  Last-Modified: Tue, 02 Feb 2016 00:51:58 GMT
Length: unspecified [text/html]
Saving to: ‘index.html?next=%2F’

index.html?next=%2F             [      <=>                                   ]  10,83K  2,93KB/s    in 3,7s    

2016-02-01 22:52:03 (2,93 KB/s) - ‘index.html?next=%2F’ saved [11085]
$wget--服务器响应'http://www.noobmovies.com/accounts/login/?next=/'
--2016-02-01 22:51:55--  http://www.noobmovies.com/accounts/login/?next=/
解析www.noobmovies.com(www.noobmovies.com)。。。69.164.217.90
连接到www.noobmovies.com(www.noobmovies.com)| 69.164.217.90 |:80。。。有联系的。
HTTP请求已发送,正在等待响应。。。
HTTP/1.1200ok
服务器:nginx/1.4.6(Ubuntu)
日期:2016年2月2日星期二00:51:58 GMT
内容类型:text/html;字符集=utf-8
传输编码:分块
连接:保持活力
改变:接受编码
到期时间:2016年2月2日星期二00:51:58 GMT
更改:Cookie,接受编码
缓存控制:最大年龄=0
设置Cookie:csrftoken=XJ07sWhMpT1hqv4K96lXkyDWAYIFt1W5;expires=2017年1月31日星期二00:51:58 GMT;最大年龄=31449600;路径=/
最后修改:2016年2月2日星期二00:51:58 GMT
长度:未指定[text/html]
保存到:'index.html?下一步=%2F'
html?下一个=%2F[]10,83K 2,93KB/s在3,7s中
2016-02-01 22:52:03(2,93 KB/s)-“index.html?next=%2F”已保存[11085]

注意
设置Cookie
行。

这些请求Cookie是由谷歌添加的吗?你怎么能从响应中得到随请求一起发送的cookies?@dm03514,我不确定。我在safari浏览器(支持yahoo搜索)中检查了同样的内容,并得到了相同的cookies列表。你能详细说明你所说的第二部分吗?