簡單的爬蟲,爬取整個頁面,修改代碼中的url可爬取指定網站,
import urllib.request # 匯入包
def getHtml(url): # 獲取html的內容
html = urllib.request.urlopen(url).read() # bytes 如果不用read()html會是一個↓
return html # http.client.HTTPResponse的變數
def saveHtml(fileName, fileContent):
with open(fileName, "wb") as f: # 以wb打開檔案
f.write(fileContent) # 寫入
def main():
url = "https://www.zhihuishu.com/" # url
html = getHtml(url) # 呼叫函式獲取bytes
saveHtml("theHtml.html", html) # 保存
print("保存網頁完成") # 提示語
if __name__ == "__main__": # 主函式
main()
遺留問題:有不少網站有反爬蟲機制,導致爬到的檔案并非是想要的檔案,
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/195287.html
標籤:其他
上一篇:python音頻播放——利用playsound和Thread庫在子執行緒播放
下一篇:Numpy 數學函式及邏輯函式
