- urllib是python內置的請求庫。它主要包含四個模塊:
- request :是最基本的HTTP請求模塊,可以用來模擬發送請求。
- error:異常處理模塊,如果請求出現錯誤,可以捕獲異常,然后進行其他操作,保證程序不會意外終止。
- parse:工具模塊,提供了很多URL處理方法,比如拆分、解析、合並等。
- robotparser:主要用來識別網站的robots.txt文件,然后判斷哪些網站可以爬。
- 使用urllib的request模塊中的方法urlopen抓取python官網,這樣我們想要的東西就可以提取出來了
import urllib.request response = urllib.request.urlopen('https://www.python.org') print(type(response)) #類型 print(response.read().decode('utf-8'))