我正在使用请求来获取网页,我想将cookie保存在Netscape风格的cookies.txt文件中。我怎样才能做到这一点?我尝试过以下方法:
import requests
import cookielib
url = 'http://www.yahoo.com'
ua = ("Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:21.0) "
"Gecko/20100101 Firefox/21.0")
headers = {'user-agent': ua}
mcj = cookielib.MozillaCookieJar()
requests.get(url, headers=headers, cookies=mcj)
mcj.save('cookies.txt')
print open('cookies.txt').read()
但它只创建一个没有cookie的cookie文件,因此输出为:
# Netscape HTTP Cookie File
# http://www.netscape.com/newsref/std/cookie_spec.html
# This is a generated file! Do not edit.