Python系列之 - 怎么用urllib.request模块下载hao123html主页

Python系列之 - 怎么用urllib.request模块下载hao123html主页


  • 下载hao123 html主页代码片段如下:
import urllib.request
  
def getHtml(url):
    html = urllib.request.urlopen(url).read()
    return html

def saveHtml(file_name, file_content):
    with open(file_name.replace('/', '_') + ".html", "wb") as f:
        f.write(file_content)

downloadURL = "https://www.hao123.com/"
html = getHtml(downloadURL)
saveHtml("hao123", html)

print("download successfully!")
  • 运行 ‘python3 downloadHao123HTML.py’ 命令下载hao123 html主页

  • 确认hao123.html下载成功并双击打开该下载网页,如下图所示:

猜你喜欢

转载自blog.csdn.net/u011537073/article/details/85729814
今日推荐