如何在 python 脚本中接受 cookie?
最佳答案
试试这个:
import urllib2
import cookielib
jar = cookielib.FileCookieJar("cookies")
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(jar))
print "Currently have %d cookies" % len(jar)
print "Getting page"
response = opener.open("http://google.com")
print response.headers
print "Got page"
print "Currently have %d cookies" % len(jar)
print jar
它应该打印
Currently have 0 cookies
...
Currently have 2 cookies
(谷歌总是设置一个 cookie)。除非您想将 cookie 保存到磁盘并在以后使用它们,否则您实际上并不需要这么多。你应该会发现
urllib2.build_opener(HTTPCookieProcessor).open(url)
满足您的大部分需求。
更多信息在这里:
关于python - 在 Python 中接受 Cookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/525773/