pycurl包是一个libcurl的python接口,由c语言编写的,功能强大,速度快。由于pycurl的属性和方法太多了,写这篇博文记录一下pycurl的属性和方法。

正常安装

pip install pycurl

如果出现问题,可以按照系统版本搜索安装方法,比如centos7.1 安装pycurl

通用请求方法

import pycurl,urllib
from io import bytesio
 
url = 'http://www.baidu.com'
 
headers = [
	"user-agent:mozilla/5.0 (iphone; cpu iphone os 5_0 like mac os x) applewebkit/534.46 (khtml, like gecko) version/5.1 mobile/9a334 safari/7534.48.3",
]
 
data = {
	"citylistname":"",
	"trade": ""
	}
 
c = pycurl.curl()	#通过curl方法构造一个对象
#c.setopt(pycurl.referer, 'http://www.baidu.com/')	#设置referer
c.setopt(pycurl.followlocation, true)	#自动进行跳转抓取
c.setopt(pycurl.maxredirs,5)			#设置最多跳转多少次
c.setopt(pycurl.connecttimeout, 60)		#设置链接超时
c.setopt(pycurl.timeout,120)			#下载超时
c.setopt(pycurl.encoding, 'gzip,deflate')	#处理gzip内容
# c.setopt(c.proxy,ip)	# 代理
c.fp = bytesio()	
c.setopt(pycurl.url, url)	#设置要访问的url
c.setopt(pycurl.httpheader,headers)		#传入请求头
c.setopt(pycurl.post, 1)
c.setopt(pycurl.postfields, urllib.urlencode(data))		#传入post数据
c.setopt(c.writefunction, c.fp.write)	#回调写入字符串缓存
c.perform()		
 
code = c.getinfo(c.http_code)	#返回状态码
html = c.fp.getvalue()	#返回源代码
 
print c.getinfo(c.total_time)

get请求方法

c = pycurl.curl()   #通过curl方法构造一个对象
c.setopt(pycurl.followlocation, true)   #自动进行跳转抓取
c.setopt(pycurl.maxredirs,5)            #设置最多跳转多少次
c.setopt(pycurl.connecttimeout, 60)     #设置链接超时
c.setopt(pycurl.timeout,120)            #下载超时
c.setopt(pycurl.encoding, 'gzip,deflate')   #处理gzip内容
# c.setopt(c.proxy,ip)  # 代理
c.fp = bytesio()  
c.setopt(pycurl.url, url)   #设置要访问的url
c.setopt(pycurl.useragent,ua) #传入ua
# c.setopt(pycurl.httpheader,self.headers)     #传入请求头
c.setopt(c.writefunction, c.fp.write)   #回调写入字符串缓存
c.perform()     
code = c.getinfo(c.http_code)   #返回状态码
html = c.fp.getvalue()  #返回源代码

post请求方法

c = pycurl.curl()   #通过curl方法构造一个对象
c.setopt(pycurl.followlocation, true)   #自动进行跳转抓取
c.setopt(pycurl.maxredirs,5)            #设置最多跳转多少次
c.setopt(pycurl.connecttimeout, 60)     #设置链接超时
c.setopt(pycurl.timeout,120)            #下载超时
c.setopt(pycurl.encoding, 'gzip,deflate')   #处理gzip内容
# c.setopt(c.proxy,ip)  # 代理
c.fp = bytesio()  
c.setopt(pycurl.url, url)   #设置要访问的url
c.setopt(pycurl.useragent,ua ) #传入user-agent
# c.setopt(pycurl.httpheader,headers)     #传入请求头
c.setopt(pycurl.post, 1)
c.setopt(pycurl.postfields, urllib.parse.urlencode(data))
c.setopt(c.writefunction, c.fp.write)   #回调写入字符串缓存
c.perform()     
code = c.getinfo(c.http_code)   #返回状态码
html = c.fp.getvalue()  #返回源代码

windows访问https

windows 访问https的方法,需要证书

import certifi
c.setopt(pycurl.cainfo, certifi.where())

获取网页多重跳转之后的地址

c.getinfo(pycurl.effective_url) 获取网页的最终地址

记录cookie

c.setopt(pycurl.cookiefile, "cookie_file_etherscan") #读取cookie
c.setopt(pycurl.cookiejar, "cookie_file_etherscan") #设置cookie

其他属性

pycurl的部分api:

pycurl.curl() #创建一个pycurl对象的方法
pycurl.curl(pycurl.url, http://www.google.com.hk) #设置要访问的url
pycurl.curl().setopt(pycurl.maxredirs, 5) #设置最大重定向次数
pycurl.curl().setopt(pycurl.connecttimeout, 60)
pycurl.curl().setopt(pycurl.timeout, 300) #连接超时设置
pycurl.curl().setopt(pycurl.useragent, "mozilla/4.0 (compatible; msie 6.0; windows nt 5.1; sv1; .net clr 1.1.4322)") #模拟浏览器
pycurl.curl().perform() #服务器端返回的信息
pycurl.curl().getinfo(pycurl.http_code) #查看http的状态 类似urllib中status属性
 
 
pycurl.namelookup_time 域名解析时间
pycurl.connect_time 远程服务器连接时间
pycurl.pretransfer_time 连接上后到开始传输时的时间
pycurl.starttransfer_time 接收到第一个字节的时间
pycurl.total_time 上一请求总的时间
pycurl.redirect_time 如果存在转向的话,花费的时间
pycurl.http_code http 响应代码
pycurl.redirect_count 重定向的次数
pycurl.size_upload 上传的数据大小
pycurl.size_download 下载的数据大小
pycurl.speed_upload 上传速度
pycurl.header_size 头部大小
pycurl.request_size 请求大小
pycurl.content_length_download 下载内容长度
pycurl.content_length_upload 上传内容长度
pycurl.content_type 内容的类型
pycurl.response_code 响应代码
pycurl.speed_download 下载速度
pycurl.info_filetime 文件的时间信息
pycurl.http_connectcode http 连接代码

参考文档

到此这篇关于python pycurl的属性与方法案例详解的文章就介绍到这了,更多相关python pycurl的属性与方法内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!