urllib简单介绍

 

# urllib简介:
1.urllib模块是Python的一个请求模块
2.Python2中是urllib和urllib2相结合实现请求的发送. Python3中统一为urllib库
3.urllib是Python内置的请求库, 其包含4个模块:
(1).request模块: 模拟发送请求
(2).error模块: 异常处理模块
(3).parse模块: 工具模块, 提供关于URL的处理方法, 如拆分, 解析, 合并等
(4).robotparser模块: 识别robots协议

# 部分方法使用介绍:
# urlopen方法实现get请求:
from urllib import request
url = 'https://www.python.org'
res = request.urlopen(url)
print(res.read())
with open('python.html', 'w') as f:
  f.write(res.read().decode('utf-8'))

# post请求:
import urllib.request
import urllib.parse
url='https://fanyi.baidu.com/sug'
postdata=urllib.parse.urlencode({'kw':'boy'}).encode('utf-8')
res = urllib.request.urlopen(url, data=postdata)
print(res.read())

# urlretrive实现下载:
from urllib.request import urlretrieve
urlretrieve('https://www.dxsabc.com/api/xiaohua/upload/min_img/20190213/20190213
XTUcIZ99B9.jpg', 'bing.jpg')

 

posted @ 2020-02-26 13:23  关不上门  阅读(524)  评论(0编辑  收藏  举报