欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

20190715《Python网络数据采集》第 1 章

程序员文章站 2022-05-29 10:19:43
1. 爬虫常见得异常及处理方法,用一个简单得爬虫代码解释,核心知识点: (1)异常一:网页在服务器上不存在(或者获取页面时,出现错误)。该异常发生时,程序会返回HTTP错误,如“404 Page Not Found” "500 Internet Server Error"等。 (2)异常二:服务器不 ......

1. 爬虫常见得异常及处理方法,用一个简单得爬虫代码解释,核心知识点:

(1)异常一:网页在服务器上不存在(或者获取页面时,出现错误)。该异常发生时,程序会返回http错误,如“404 page not found” "500 internet server error"等。

(2)异常二:服务器不存在(即,链接打不开,或者url链接写错了),这时,urlopen会返回一个none对象。

ps:有的时候,网页已经从服务器成功获取,如果网页上的内容并非完全是我们期望的那样,也会出现异常。


 1 from urllib.request import urlopen
 2 from bs4 import beautifulsoup
 3 
 4 try:
 5     html = urlopen("http://pythonscraping.com/pages/page1.html")
 6 # print(html.read())
 7 # 检测:网页在服务器上是否存在(或者获取页面时是否出现错误)
 8 except httperror as e:
 9     print(e)
10 else:
11     bsobj = beautifulsoup(html.read())
12     # 检测:服务器是否存在(就是说链接能否打开,或者是url链接写错了)
13     if html is none:
14         print("url is not found")
15     else:
16         print(bsobj.h1)
17         # print(bsobj.title)

 

 1 # 以上代码更改为检测异常更全面、可读性更强的代码,如下:
 2 from urllib.request import urlopen
 3 from bs4 import beautifulsoup
 4 
 5 def gettitle(url):
 6     try:
 7         html = urlopen(url)
 8     except httperror as e:
 9         return none
10     try:
11         bsobj = beautifulsoup(html.read())
12         title = bsobj.body.h1
13     except attributeerror as e:
14         return
15     return title
16 
17 title1 = gettitle("http://pythonscraping.com/pages/page1.html")
18 if title1 == none:
19     print("title could not be found")
20 else:
21     print(title1)

 该部分代码执行时,出现报错:

 indentationerror: unexpected indent process finished with exit code 1

 google发现,tag和space不能混合使用。原始第五行,def被tab缩进,后删除该tab缩进,问题解决。该问题具体原因,仍需要仔细查明!!!