urllib是python内置的http客户端库,用于网络请求相关的主要是urllib.request子模块。
GET请求
from urllib import request
# 发送GET请求并获取响应
response = request.urlopen('http://httpbin.org/get')
# 读取响应内容
html = response.read().decode('utf-8')
print(html)
#输出
{
"args": {},
"headers": {
"Accept-Encoding": "identity",
"Host": "httpbin.org",
"User-Agent": "Python-urllib/3.8",
"X-Amzn-Trace-Id": "Root=1-6820512b-44adb7d37a1f15233ad07016"
},
"origin": "6x.xxx.xx.xx",
"url": "http://httpbin.org/get"
}