Python PycURL自动处理cookie的方法,pythonpycurl,本节要为大家讲下如何使用


本节要为大家讲下如何使用Python PycURL库自动处理cookie的方法实现代码。

首先大家要了解下pycurl是什么?pycurl是功能强大的python的url库,但它是用c语言编写的。运行速度很快,如果和python urllib、httplib做比较的话,要比这两者都快。

在使用Python PycURL库之前,你要确保它已经安装了。

import pycurlimport 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#www.iplaypy.com# Note: must be a string, not a file object.crl.setopt(pycurl.COOKIEJAR, "cookie_file_name") crl.perform()print crl.fp.getvalue()

编橙之家文章,

评论关闭