在Windows使用Python的urllib3库【20210724】

发表于 2020-09-24 15:00:34
阅读 57

介绍

介绍

福哥在使用urllib库访问API接口的时候,发现一个很奇葩的问题,就是网上满天飞的教程里都是教大家如何使用urllib的,但是通过pip安装urllib提示找不到软件包,这是怎么回事?后来又看到有人使用urllib2来替换urllib库,使用pip尝试安装一下,发现urllib2也是找不到软件包,啊?怎么办?再后来又发现有人使用urllib3来替换urllib和urllib2,再使用pip尝试安装一下,发现urllib3还是找不到,崩溃了!最后看到有人说urllib和urllib2是自带的,不需要安装了,那么是不是urllib3也不需要安装呢?直接import一下,发现居然成功了!好吧,总算是用起来了~~

福哥对比urllib和urllib2以及最新版本的urllib3的语法,发现urllib3还是最完善的,怪不得要替换掉urllib和urllib2了,那么童鞋们就跟福哥学习一下urllib3的使用方法吧。

GET

直接GET

直接请求一个网址的内容

import urllib3

# argv
url = "http://www.baidu.com"

# load url by get
http = urllib3.PoolManager()
res = http.request('GET',url)
status = res.status
source_uc = res.data.decode("utf-8")
source = source_uc.encode("gbk","ignore")

print ("状态码:" + str(status))
print ("源代码:" + source)

带参数GET

带参数请求一个网址的内容,在百度里搜索“site:tongfu.net”

import urllib3
import re

# argv
url = "http://www.baidu.com/s"

# load url by get
http = urllib3.PoolManager()
res = http.request('GET',url,fields={'wd':"site:tongfu.net"})
status = res.status
source_uc = res.data.decode("utf-8")
source = source_uc.encode("gbk","ignore")

print ("状态码:" + str(status))

regexp = re.compile("同福主页 - 首页 - 同福网 - TONGFU.net", re.M)
mats = regexp.search(source)
if mats:
    print (mats.group())
else:
    print ("没有找到")

d68aca19c00f1b18.jpg

可以看出我们通过传递wd参数,查询到百度,得到了查询结果

POST

POST普通数据

直接通过POST方式请求一个网址并提交一组数据

import urllib3
import re

# argv
url = "http://www.baidu.com/s"

# load url by get
http = urllib3.PoolManager()
res = http.request('POST',url,fields={'wd':"site:tongfu.net"})
status = res.status
source_uc = res.data.decode("utf-8")
source = source_uc.encode("gbk","ignore")

print ("状态码:" + str(status))

regexp = re.compile("同福主页 - 首页 - 同福网 - TONGFU.net", re.M)
mats = regexp.search(source)
if mats:
    print (mats.group())
else:
    print ("没有找到")

8b281341496bdd55.jpg

可以看出使用POST方式传递wd参数,百度是不认的,查询不到结果

POST Json数据

通过POST方式请求一个网址并以json格式提交一组数据

编码JSON数据使用json.dumps,这个是有失败情况的,所以需要在try下使用

import urllib3
import json

# argv
url = "http://www.baidu.com/s"

# load url by get
http = urllib3.PoolManager()
try:
    data = {'wd':"site:tongfu.net"}
    json_data = json.dumps(data)
except Exception as e:
    doNothing = e
res = http.request('POST',url,body=json_data,headers={'Content-Type':"application/json"})
status = res.status
source_uc = res.data.decode("utf-8")
source = source_uc.encode("gbk","ignore")

print status
print source

JSON

解析JSON数据

一般情况下,接口返回的数据都是json格式的,我们需要学习如何处理json数据

解析JSON数据使用json.loads,这个是有失败情况的,所以需要在try下使用

import urllib3
import json

# argv
url = "https://tongfu.net/api/demo/user"

# load url by get
http = urllib3.PoolManager()
res = http.request('POST',url)
status = res.status
source_uc = res.data.decode("utf-8")
source = source_uc.encode("gbk","ignore")
try:
    source_json = json.loads(source_uc)
except Exception as e:
    doNothing = e

print status
print source

print source_json.get('errcode')
print source_json.get('errmsg')
print source_json.get('data').get('total')
print source_json.get('data').get('datas')[0].get('title')

认证

http认证

使用urllib3实现http认证的方法

import urllib3
import json

# argv
url = "http://www.baidu.com/s"

# load url by get
http = urllib3.PoolManager()
try:
    data = {'wd':"site:tongfu.net"}
    json_data = json.dumps(data)
except Exception as e:
    doNothing = e
headers_data = urllib3.util.make_headers(basic_auth="demo:123456")
headers_data['Content-Type'] = "application/json"
res = http.request('POST',url,body=json_data,headers=headers_data)
status = res.status
source_uc = res.data.decode("utf-8")
source = source_uc.encode("gbk","ignore")

print status
print source

Python3

如果要在python3下使用urllib3还是需要单独安装的,可以使用如下命令完成urllib3的安装

pip3 install urllib3

119cbf2b8ec706af.jpg

总结

可以看到urllib3是个非常强大的软件包,基本可以完成模拟绝大部分网络操作的需要。童鞋们需要掌握这一技术,因为没有网络的软件的功能是有限的,没有网络的游戏是没有灵魂的,无论我们编写什么样的程序,脱离网络基本都是小玩闹了。