1. 程式人生 > >Python系列之 - 怎麼用urllib.request模組下載hao123html主頁

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下載成功並雙擊開啟該下載網頁,如下圖所示: