urllib模块

urllib是python3中的内置模块,无需安装即可直接使用

基本使用

这里我们主要给大家演示如何发送请求和接收响应

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
def test01():
    """
    发送请求
    """
    url = "http://www.baidu.com"
    response = urllib.request.urlopen(url)

    print("获取响应状态码:",response.getcode())
    print("获取所有响应头:",response.getheaders())
    print("获取指定响应头:",response.getheader("Content-Type"))
    print("获取响应体:",response.read().decode())

发送请求的时候,我们还可以携带请求头信息

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
def test02():
    """
        发送信息带有请求头
    """
    url = "http://httpbin.org/headers"
    headers = {"User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/22.0.1207.1 Safari/537.1"}
    request = urllib.request.Request(url,headers=headers)

    response = urllib.request.urlopen(request)
    print(response.read().decode())

get请求

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
def test03():
    """
    发送带参数的get请求
    """
    url = "http://www.baidu.com/s?"
    data = urllib.parse.urlencode({"wd":"小凯"})
    # 编码之后将参数拼接到url末尾
    url += data
    # 构建请求对象
    req = urllib.request.Request(url)
    # 发送请求
    response = urllib.request.urlopen(req)
    print(response.read().decode())

post请求

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
def test05():
    """
    发送post请求带参数
    """
    url = "http://www.httpbin.org/post"
    # 对参数进行编码
    data = urllib.parse.urlencode({"wd":"小凯"}).encode()
    # 构建请求对象
    req = urllib.request.Request(url,data)
    # 发送请求
    response = urllib.request.urlopen(req)
    # 打印response中的信息
    print(response.read().decode())