Python爬虫——写出最简单的网页爬虫

开发 后端
最近对python爬虫有了强烈地兴趣,在此分享自己的学习路径,欢迎大家提出建议。我们相互交流,共同进步。

最近对python爬虫有了强烈地兴趣,在此分享自己的学习路径,欢迎大家提出建议。我们相互交流,共同进步。

1.开发工具

笔者使用的工具是sublime text3,它的短小精悍(可能男人们都不喜欢这个词)使我十分着迷。推荐大家使用,当然如果你的电脑配置不错,pycharm可能更加适合你。

sublime text3搭建python开发环境推荐查看此博客:

[sublime搭建python开发环境][http://www.cnblogs.com/codefish/p/4806849.html]

2.爬虫介绍

爬虫顾名思义,就是像虫子一样,爬在Internet这张大网上。如此,我们便可以获取自己想要的东西。

既然要爬在Internet上,那么我们就需要了解URL,法号“统一资源定位器”,小名“链接”。其结构主要由三部分组成:

(1)协议:如我们在网址中常见的HTTP协议。

(2)域名或者IP地址:域名,如:www.baidu.com,IP地址,即将域名解析后对应的IP。

(3)路径:即目录或者文件等。

3.urllib开发最简单的爬虫

(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)开发最简单的爬虫

百度首页简洁大方,很适合我们爬虫。

爬虫代码如下:

  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()  

结果如下图:

 

我们可以通过在百度首页空白处右击,查看审查元素来和我们的运行结果对比。

当然,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()  

运行结果和刚才相同。

(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()  

运行结果如图: 

 

404为打印出的错误代码,关于此详细信息大家可以自行百度。

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()  

运行结果如图: 

 

既然为了处理错误,那么***两个错误都写入代码中,毕竟越细致越清晰。须注意的是,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
相关推荐

2010-03-09 09:32:20

Python网页爬虫

2019-12-27 18:07:53

Python网络爬虫HTML

2012-05-10 13:42:26

Java网络爬虫

2010-03-03 09:30:40

Python实现网页爬

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

2017-08-22 17:30:14

Python爬虫

2023-11-28 08:34:39

Python工具

2017-06-14 15:20:43

Python爬虫BeautifulSo

2018-07-02 14:12:26

Python爬虫反爬技术

2022-11-24 10:24:32

2016-10-21 14:35:52

Pythonwebget方法

2019-06-18 10:49:41

Python技术web

2016-10-20 20:21:09

Python爬虫技巧

2016-10-13 15:51:50

2018-01-29 09:28:44

2022-09-14 23:06:45

2017-06-19 15:32:39

Python爬虫音频数据
点赞
收藏

51CTO技术栈公众号