python爬⾍之urllib3的使⽤⽰例
Urllib3是⼀个功能强⼤,条理清晰,⽤于HTTP客户端的Python库。许多Python的原⽣系统已经开始使⽤urllib3。Urllib3提供了很多python标准库urllib⾥所没有的重要特性:
1. 线程安全
2. 连接池
3. 客户端SSL/TLS验证
4. ⽂件分部编码上传
5. 协助处理重复请求和HTTP重定位
6. ⽀持压缩编码
7. ⽀持HTTP和SOCKS代理
⼀、get请求
urllib3主要使⽤连接池进⾏⽹络请求的访问,所以访问之前我们需要创建⼀个连接池对象,如下所⽰:
import urllib3
url = ""
http = urllib3.PoolManager();
r = quest('GET',url+"/get")
print(r.data.decode())
print(r.status)
带参数的get
r = quest('get','www.baidu/s',fields={'wd':'周杰伦'})
print(r.data.decode())
writelines在python中的用法经查看源码:
def request(self, method, url, fields=None, headers=None, **urlopen_kw):
第⼀个参数method 必选,指定是什么请求,'get'、'GET'、'POST'、'post'、'PUT'、'DELETE'等,不区分⼤⼩写。
第⼆个参数url,必选
第三个参数fields,请求的参数,可选
第四个参数headers 可选
request请求的返回值是&sponse.HTTPResponse object at 0x000001B3879440B8>
我们可以通过dir()查看其所有的属性和⽅法。
dir(r)
直截取了⼀部分
#'data', 'decode_content', 'enforce_content_length', 'fileno', 'flush', 'from_httplib',
# 'get_redirect_location', 'getheader', 'getheaders', 'headers', 'info', 'isatty',
# 'length_remaining', 'read', 'read_chunked', 'readable', 'readinto', 'readline',
# 'readlines', 'reason', 'release_conn', 'retries', 'seek', 'seekable', 'status',
# 'stream', 'strict', 'supports_chunked_reads', 'tell', 'truncate', 'version', 'writable',
# 'writelines']
⼆、post请求
import urllib3
url = ""
fields = {
'name':'xfy'
}
http = urllib3.PoolManager()
r = quest('post',url+"/post",fields=fields)
print(r.data.decode())
可以看到很简单,只是第⼀个参数get换成了post。
并且参数不需要再像urllib⼀样转换成byte型了。
三、设置headers
import urllib3
headers = {
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36' }
http = urllib3.PoolManager();
r = quest('get',url+"/get",headers = headers)
print(r.data.decode())
四、设置代理
import urllib3
url = ""
headers = {
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36' }
proxy = urllib3.ProxyManager('101.236.19.165:8866',headers = headers)
r = quest('get',url+"/ip")
print(r.data.decode())
五、当请求的参数为json
在发起请求时,可以通过定义body 参数并定义headers的Content-Type参数来发送⼀个已经过编译的JSON数据import urllib3
url = ""
import json
data = {'name':'徐繁韵'}
json_data = json.dumps(data)
http = urllib3.PoolManager()
r = quest('post',url+"/post",body = json_data,headers = {'Content-Type':'application/json'})
print(r.data.decode('unicode_escape'))
六、上传⽂件
#元组形式
with open('a.html','rb') as f:
data = f.read()
http = urllib3.PoolManager()
r = quest('post','/post',fields = {'filefield':('a.html',data,'text/plain')})
print(r.data.decode())
#⼆进制形式
r = quest('post','/post',body = data,headers={'Content-Type':'image/jpeg'})
print(r.data.decode())
七、超时设置
# 1全局设置超时
# http = urllib3.PoolManager(timeout = 3)
# 2在request⾥设置
# quest('post','/post',timeout = 3)
⼋、重试和重定向
import urllib3
http = urllib3.PoolManager()
#重试
r = quest('post','/post',retries = 5) #请求重试测次数为5次,默认为3ci
ies) #Retry(total=5, connect=None, read=None, redirect=0, status=None)
#关闭重试
r = quest('get','/redirect/1',redirect = False)
ies)# Retry(total=3, connect=None, read=None, redirect=None, status=None)
print(r.status)
print(r.data.decode())
print("--------------------")
_redirect_location())
#302不是异常
九、urllib3 本⾝设置了https的处理,但是有警告
虽然可以请求,但是报如下警告:
禁⽤警告:
import urllib3
urllib3.disable_warnings() #禁⽤各种警告
url = "www.12306/mormhweb/"
http = urllib3.PoolManager()
r = quest('get',url)
print(r.data.decode())
urllib3很强⼤,但是并没有requests好⽤。了解为主。
以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。