一、原始碼
使用request庫爬取整個網頁
1 import requests 2 # encoding:utf-8 #默認格式utf-8 3 4 def get_html(url): #爬取原始碼函式 5 headers = { 6 'User-Agent': 'Mozilla/5.0(Macintosh; Intel Mac OS X 10_11_4)\ 7 AppleWebKit/537.36(KHTML, like Gecko) Chrome/52 .0.2743. 116 Safari/537.36' 8 9 } # 模擬瀏覽器訪問 10 response = requests.get(url, headers=headers) # 請求訪問網站 11 response.encoding = response.apparent_encoding #設定字符編碼格式 12 html = response.text # 獲取網頁原始碼 13 return html # 回傳網頁原始碼 14 15 r = get_html('https://www.baidu.com/') 16 print(r) #列印網頁原始碼
二、運行結果

三、request庫介紹 點擊這里
\\我的python爬蟲筆記\\~~持續更新中
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/5079.html
標籤:Python
