urllib
https://www.cnblogs.com/strivepy/p/9231127.html
urllib是python内置的http请求库(基本请求库 ,requests库就是通过urllib是实现的),各种功能相比较之下也是比较完备的,urllib库包含了一下四个模块:
urllib.request 请求模块
urllib.error 异常处理模块
urllib.parse url解析模块
urllib.robotparse robots.txt解析模块
import urllib.request
response=urllib.request.urlopen('http://www.baidu.com')
#使用read()方法得到响应体内容,这时是一个字节流bytes,看到明文还需要decode为charset格式
print(response.read().decode('utf-8'))
print(response.status)
-------------------------------------
import urllib.request
response=urllib.request.urlopen('https://www.python.org')
print(response.read().decode('utf-8'))
print(response.status)
print(response.getheaders())
-----------------------------------
import urllib.request
import urllib.parse
data={'name':'zhangsan'}
data = urllib.parse.urlencode(data).encode('utf-8')
response=urllib.request.urlopen('http://httpbin.org/post',data=data)
print(response.read().decode('utf-8'))
print(response.status)
print(response.getheaders())

浙公网安备 33010602011771号