1、什么是urllib?

urllib是python内置的http请求库,主要有4个模块,分别是:urllib.request(请求模块)、urllib.error(异常处理模块)、urlib.parse(url解析模块)、urllib.robotparser(robots.txt解析模块)

2、urllib用法讲解之urllib.request

import urllib.request
response = urllib.request.urlopen('http://www.baidu.com')
print(response.read().decode('utf-8'))

import urllib.parse
import urllib.request
data = bytes(urllib.parse.urlencode({'word':'hello'}),encoding='utf-8')
response = urllib.request.urlopen('http://httpbin.org/post', data=data)
print(response.read())

import urllib.request
response = urllib.request.urlopen('http://httpbin.org/get', timeout=1)
print(response.read())

#演示urllib.error
import socket
import urllib.request
import urllib.error
try:
response = urllib.request.urlopen('http://httpbin.org/get', timeout=0.1)
except urllib.error.URLError as e:
if isinstance(e.reason, socket.timeout):
print('TIME OUT')

2、response响应

#演示响应类型
import urllib.request
response = urllib.request.urlopen('http://www.python.org')
print(type(response))

#演示状态头、响应头
import urllib.request
response = urllib.request.urlopen('http://www.python.org')
print(response.status)
print(response.getheaders())
print(response.getheader('server'))

3、request

#演示request
import urllib.request
request = urllib.request.Request('http://www.python.org')
response = urllib.request.urlopen(request)
print(response.read().decode('utf-8'))

4、handler


urllib用法讲解之urllib.error

urllib用法讲解之urllib.urlparse




官方文档参考地址:https://docs.python.org/zh-cn/3/library/urllib.html