python的requests模塊爬取網頁內容


注意:處理需要用戶名密碼認證的網站,需要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表示成功

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM