urllib库的使用

#使用urllib库,将langlang2017全站网页请求并保存
#1、引入模块
from urllib import request
from urllib import error



#2、操作
#(1)创建url
base_url = "http://www.langlang2017.com/route.html"

try:
    # (2)请求url
    reponse = request.urlopen(base_url,timeout=0.02)

    # (3)读取内容
    html = reponse.read()

    # (4)转码
    html = html.decode("utf-8")
    # (5)保存
    with open("route.html", "w", encoding="utf-8") as f:
        f.write(html)

except error.URLError as e:
    print(e)
原文地址:https://www.cnblogs.com/zhangboblogs/p/8542041.html