python pycurl模块自动处理cookie

发布时间:2020-12-12编辑:脚本学堂
python pycurl模块自动处理cookie信息的方法,python pycurl模块用法的小例子。

代码:
 

复制代码 代码示例:

#!/usr/bin/env python
# python pycurl模块

import pycurl
import StringIO

url = "http://www.google.com/"
crl = pycurl.Curl()
crl.setopt(pycurl.VERBOSE,1)
crl.setopt(pycurl.FOLLOWLOCATION, 1)
crl.setopt(pycurl.MAXREDIRS, 5)
crl.fp = StringIO.StringIO()
crl.setopt(pycurl.URL, url)
crl.setopt(crl.WRITEFUNCTION, crl.fp.write)

# Option -b/--cookie <name=string/file> Cookie string or file to read cookies from
# Note: must be a string, not a file object.
crl.setopt(pycurl.COOKIEFILE, "cookie_file_name")

# Option -c/--cookie-jar <file> Write cookies to this file after operation
# Note: must be a string, not a file object.
crl.setopt(pycurl.COOKIEJAR, "cookie_file_name")

crl.perform()
print crl.fp.getvalue()