Anu*_*nuj 5 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.cookies.get_dict()
print allcookies
Run Code Online (Sandbox Code Playgroud)
使用这个我只得到如下的csrftoken:
{'csrftoken': 'ePE8zGxV4yHJ5j1NoGbXnhLK1FQ4jwqO'}
Run Code Online (Sandbox Code Playgroud)
但在谷歌浏览器中,我看到除了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
Run Code Online (Sandbox Code Playgroud)
输出:
<!DOCTYPE html>
<html xmlns="http://www.w3.org
the cookies are:
<Cookie csrftoken=ePE8zGxV4yHJ5j1NoGbXnhLK1FQ4jwqO for www.noobmovies.com/>
Run Code Online (Sandbox Code Playgroud)
那么,我怎样才能获得所有的cookie?谢谢.
正在设置的 cookie 来自其他页面/资源,可能由 JavaScript 代码加载。您可以使用wget、curl或httpie等工具仅向页面发出请求(不运行 JS 代码)来检查它。
\n\n该服务器设置的唯一 cookie 是csrftoken
,如您所见:
$ wget --server-response \'http://www.noobmovies.com/accounts/login/?next=/\'\n--2016-02-01 22:51:55-- http://www.noobmovies.com/accounts/login/?next=/\nResolving www.noobmovies.com (www.noobmovies.com)... 69.164.217.90\nConnecting to www.noobmovies.com (www.noobmovies.com)|69.164.217.90|:80... connected.\nHTTP request sent, awaiting response... \n HTTP/1.1 200 OK\n Server: nginx/1.4.6 (Ubuntu)\n Date: Tue, 02 Feb 2016 00:51:58 GMT\n Content-Type: text/html; charset=utf-8\n Transfer-Encoding: chunked\n Connection: keep-alive\n Vary: Accept-Encoding\n Expires: Tue, 02 Feb 2016 00:51:58 GMT\n Vary: Cookie,Accept-Encoding\n Cache-Control: max-age=0\n Set-Cookie: csrftoken=XJ07sWhMpT1hqv4K96lXkyDWAYIFt1W5; expires=Tue, 31-Jan-2017 00:51:58 GMT; Max-Age=31449600; Path=/\n Last-Modified: Tue, 02 Feb 2016 00:51:58 GMT\nLength: unspecified [text/html]\nSaving to: \xe2\x80\x98index.html?next=%2F\xe2\x80\x99\n\nindex.html?next=%2F [ <=> ] 10,83K 2,93KB/s in 3,7s \n\n2016-02-01 22:52:03 (2,93 KB/s) - \xe2\x80\x98index.html?next=%2F\xe2\x80\x99 saved [11085]\n
Run Code Online (Sandbox Code Playgroud)\n\n注意该Set-Cookie
线。
归档时间: |
|
查看次数: |
1295 次 |
最近记录: |