注意:處理需要用戶名密碼認證的網站,需要auth字段。
# -*- coding:utf-8 -*- import requests headers = { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36", } newUrl ="https://www.freebuf.com/articles/system/187792.html" #最簡單的爬蟲請求.也可以加上headers字段,防止部分網址的反爬蟲機制 response = requests.get(newUrl) #當爬取的界面需要用戶名密碼登錄時候,構建的請求需要包含auth字段 #response = requests.get(newUrl,headers=headers,auth=('username','passsword')) print(response.content.decode("utf-8"))#打印網頁內容 #print(response.status_code)#瀏覽器返回的錯誤碼,200表示成功
