urllib模块的用法

介绍:urllib 是 python3.X中提供的一系列操作URL的库,它可以轻松的模拟用户使用浏览器访问网页

使用步骤:
1.导入 urllib 库的 request 模块

import urllib.request

2.先请求一个url,百度例子

url = "http://www.baidu.com/"

3.处理http请求

handle = urllib.request.HTTPHandler()

4.处理https请求的hanlder

opener = urllib.request.build_opener(handle)

5.# 第二步,创建一个opener

request = urllib.request.Request("http://www.baidu.com/")

6.# 创建一个请求

response = opener.open(request)

7.发送请求

content = response.read().decode()

8.读取,w读取

with open("baidu.html", "w") as f:
    f.write(content)

你可能感兴趣的:(urllib模块的用法)