自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Python爬蟲——寫出最簡單的網(wǎng)頁爬蟲

開發(fā) 后端
最近對python爬蟲有了強烈地興趣,在此分享自己的學習路徑,歡迎大家提出建議。我們相互交流,共同進步。

最近對python爬蟲有了強烈地興趣,在此分享自己的學習路徑,歡迎大家提出建議。我們相互交流,共同進步。

1.開發(fā)工具

筆者使用的工具是sublime text3,它的短小精悍(可能男人們都不喜歡這個詞)使我十分著迷。推薦大家使用,當然如果你的電腦配置不錯,pycharm可能更加適合你。

sublime text3搭建python開發(fā)環(huán)境推薦查看此博客:

[sublime搭建python開發(fā)環(huán)境][http://www.cnblogs.com/codefish/p/4806849.html]

2.爬蟲介紹

爬蟲顧名思義,就是像蟲子一樣,爬在Internet這張大網(wǎng)上。如此,我們便可以獲取自己想要的東西。

既然要爬在Internet上,那么我們就需要了解URL,法號“統(tǒng)一資源定位器”,小名“鏈接”。其結(jié)構(gòu)主要由三部分組成:

(1)協(xié)議:如我們在網(wǎng)址中常見的HTTP協(xié)議。

(2)域名或者IP地址:域名,如:www.baidu.com,IP地址,即將域名解析后對應的IP。

(3)路徑:即目錄或者文件等。

3.urllib開發(fā)最簡單的爬蟲

(1)urllib簡介

Module Introduce
urllib.error Exception classes raised by urllib.request.
urllib.parse Parse URLs into or assemble them from components.
urllib.request Extensible library for opening URLs.
urllib.response Response classes used by urllib.
urllib.robotparser Load a robots.txt file and answer questions about fetchability of other URLs.

(2)開發(fā)最簡單的爬蟲

百度首頁簡潔大方,很適合我們爬蟲。

爬蟲代碼如下:

  1. from urllib import request 
  2.  
  3. def visit_baidu(): 
  4.     URL = "http://www.baidu.com" 
  5.     # open the URL 
  6.     req = request.urlopen(URL) 
  7.     # read the URL  
  8.     html = req.read() 
  9.     # decode the URL to utf-8 
  10.     html = html.decode("utf_8"
  11.     print(html) 
  12.  
  13. if __name__ == '__main__'
  14.     visit_baidu()  

結(jié)果如下圖:

 

我們可以通過在百度首頁空白處右擊,查看審查元素來和我們的運行結(jié)果對比。

當然,request也可以生成一個request對象,這個對象可以用urlopen方法打開。

代碼如下:

  1. from urllib import request 
  2.  
  3. def vists_baidu(): 
  4.     # create a request obkect 
  5.     req = request.Request('http://www.baidu.com'
  6.     # open the request object 
  7.     response = request.urlopen(req) 
  8.     # read the response  
  9.     html = response.read() 
  10.     html = html.decode('utf-8'
  11.     print(html) 
  12.  
  13. if __name__ == '__main__'
  14.     vists_baidu()  

運行結(jié)果和剛才相同。

(3)錯誤處理

錯誤處理通過urllib模塊來處理,主要有URLError和HTTPError錯誤,其中HTTPError錯誤是URLError錯誤的子類,即HTTRPError也可以通過URLError捕獲。

HTTPError可以通過其code屬性來捕獲。

處理HTTPError的代碼如下:

  1. from urllib import request 
  2. from urllib import error 
  3.  
  4. def Err(): 
  5.     url = "https://segmentfault.com/zzz" 
  6.     req = request.Request(url) 
  7.  
  8.     try: 
  9.         response = request.urlopen(req) 
  10.         html = response.read().decode("utf-8"
  11.         print(html) 
  12.     except error.HTTPError as e: 
  13.         print(e.code) 
  14. if __name__ == '__main__'
  15.     Err()  

運行結(jié)果如圖: 

 

404為打印出的錯誤代碼,關(guān)于此詳細信息大家可以自行百度。

URLError可以通過其reason屬性來捕獲。

chuliHTTPError的代碼如下:

  1. from urllib import request 
  2. from urllib import error 
  3.  
  4. def Err(): 
  5.     url = "https://segmentf.com/" 
  6.     req = request.Request(url) 
  7.  
  8.     try: 
  9.         response = request.urlopen(req) 
  10.         html = response.read().decode("utf-8"
  11.         print(html) 
  12.     except error.URLError as e: 
  13.         print(e.reason) 
  14. if __name__ == '__main__'
  15.     Err()  

運行結(jié)果如圖: 

 

既然為了處理錯誤,那么***兩個錯誤都寫入代碼中,畢竟越細致越清晰。須注意的是,HTTPError是URLError的子類,所以一定要將HTTPError放在URLError的前面,否則都會輸出URLError的,如將404輸出為Not Found。

代碼如下:

  1. from urllib import request 
  2. from urllib import error 
  3.  
  4. # ***種方法,URLErroe和HTTPError 
  5. def Err(): 
  6.     url = "https://segmentfault.com/zzz" 
  7.     req = request.Request(url) 
  8.  
  9.     try: 
  10.         response = request.urlopen(req) 
  11.         html = response.read().decode("utf-8"
  12.         print(html) 
  13.     except error.HTTPError as e: 
  14.         print(e.code) 
  15.     except error.URLError as e: 
  16.         print(e.reason)  

大家可以更改url來查看各種錯誤的輸出形式。

責任編輯:龐桂玉 來源: segmentfault
相關(guān)推薦

2012-05-10 13:42:26

Java網(wǎng)絡爬蟲

2010-03-09 09:32:20

Python網(wǎng)頁爬蟲

2019-12-27 18:07:53

Python網(wǎng)絡爬蟲HTML

2010-03-03 09:30:40

Python實現(xiàn)網(wǎng)頁爬

2011-02-23 09:48:00

Python.NET

2011-02-22 10:00:38

.NETc#IronPython

2020-10-19 19:25:32

Python爬蟲代碼

2011-03-18 10:25:20

javac++Python

2024-06-07 08:56:43

HTTPPythonSelenium

2017-08-22 17:30:14

Python爬蟲

2024-11-27 06:31:02

2022-11-24 10:24:32

2018-07-02 14:12:26

Python爬蟲反爬技術(shù)

2023-11-28 08:34:39

Python工具

2017-06-14 15:20:43

Python爬蟲BeautifulSo

2016-10-13 15:51:50

2018-01-29 09:28:44

2016-10-21 14:35:52

Pythonwebget方法

2019-06-18 10:49:41

Python技術(shù)web

2016-10-20 20:21:09

Python爬蟲技巧
點贊
收藏

51CTO技術(shù)棧公眾號