如果爬蟲沒有異常處理,那麼爬行中一旦出現錯誤,程式将崩潰停止工作,有異常處理即使出現錯誤也能繼續執行下去
1.常見狀态碼
301:重定向到新的URL,永久性
302:重定向到臨時URL,非永久性
304:請求的資源未更新
400:非法請求
401:請求未經授權
403:禁止通路
404:沒找到對應頁面
500:伺服器内部出現錯誤
501:伺服器不支援實作請求所需要的功能
2.異常處理
URLError捕獲異常資訊
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import urllib.request
import urllib.error
try: #嘗試執行裡面的内容
html = urllib.request.urlopen('http://www.xiaohuar.com/').read().decode("utf-8")
print(html)
except urllib.error.URLError as e: #如果出現錯誤
if hasattr(e,"code"): #如果有錯誤代碼
print(e.code) #列印錯誤代碼
if hasattr(e,"reason"): #如果有錯誤資訊
print(e.reason) #列印錯誤資訊
#傳回 說明網站禁止了爬蟲通路
# 403
# Forbidden
浏覽器僞裝技術
很多網站,做了反爬技術,一般在背景檢測請求頭資訊裡是否有User-Agent浏覽器資訊,如果沒有說明不是浏覽器通路,就屏蔽了這次請求
是以,我們需要僞裝浏覽器報頭來請求
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import urllib.request
url = 'https://www.qiushibaike.com/' #抓取頁面URL
tou = ('User-Agent','Mozilla/5.0 (Windows NT 10.0; WOW64; rv:54.0) Gecko/20100101 Firefox/54.0') #設定模拟浏覽器報頭
b_tou = urllib.request.build_opener() #建立請求對象
b_tou.addheaders=[tou] #添加報頭
html = b_tou.open(url).read().decode("utf-8") #開始抓取頁面
print(html)
注意:我們可以看到這次請求并不是用urlopen()方法請求的,此時用urlopen()無法請求,但是我們就會感覺到這樣很費勁,難道每次請求都要建立build_opener(),是以我們需要設定使用urlopen()方法請求自動報頭
設定使用urlopen()方法請求自動報頭,也就是設定使用者代理
install_opener()将報頭資訊設定為全局,urlopen()方法請求時也會自動添加報頭
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import urllib.request
#設定報頭資訊
tou = ('User-Agent','Mozilla/5.0 (Windows NT 10.0; WOW64; rv:54.0) Gecko/20100101 Firefox/54.0') #設定模拟浏覽器報頭
b_tou = urllib.request.build_opener() #建立請求對象
b_tou.addheaders=[tou] #添加報頭到請求對象
#将報頭資訊設定為全局,urlopen()方法請求時也會自動添加報頭
urllib.request.install_opener(b_tou)
#請求
url = 'https://www.qiushibaike.com/'
html = urllib.request.urlopen(url).read().decode("utf-8")
print(html)
建立使用者代理池
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import urllib.request
import random #引入随機子產品檔案
def yh_dl(): #建立使用者代理池
yhdl = [
'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-us) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50',
'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0',
'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0)',
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:2.0.1) Gecko/20100101 Firefox/4.0.1',
'Mozilla/5.0 (Windows NT 6.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1',
'Opera/9.80 (Macintosh; Intel Mac OS X 10.6.8; U; en) Presto/2.8.131 Version/11.11',
'Opera/9.80 (Windows NT 6.1; U; en) Presto/2.8.131 Version/11.11',
'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Maxthon 2.0)',
'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; TencentTraveler 4.0)',
'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)',
'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; The World)',
'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; 360SE)',
'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Avant Browser)',
'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)',
'Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_3_3 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8J2 Safari/6533.18.5',
'User-Agent:Mozilla/5.0 (iPod; U; CPU iPhone OS 4_3_3 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8J2 Safari/6533.18.5',
'Mozilla/5.0 (iPad; U; CPU OS 4_3_3 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8J2 Safari/6533.18.5',
'Mozilla/5.0 (Linux; U; Android 2.3.7; en-us; Nexus One Build/FRF91) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1',
'Opera/9.80 (Android 2.3.4; Linux; Opera Mobi/build-1107180945; U; en-GB) Presto/2.8.149 Version/11.10',
'Mozilla/5.0 (Linux; U; Android 3.0; en-us; Xoom Build/HRI39) AppleWebKit/534.13 (KHTML, like Gecko) Version/4.0 Safari/534.13',
'Mozilla/5.0 (BlackBerry; U; BlackBerry 9800; en) AppleWebKit/534.1+ (KHTML, like Gecko) Version/6.0.0.337 Mobile Safari/534.1+',
'Mozilla/5.0 (compatible; MSIE 9.0; Windows Phone OS 7.5; Trident/5.0; IEMobile/9.0; HTC; Titan)',
'UCWEB7.0.2.37/28/999',
'NOKIA5700/ UCWEB7.0.2.37/28/999',
'Openwave/ UCWEB7.0.2.37/28/999',
'Mozilla/4.0 (compatible; MSIE 6.0; ) Opera/UCWEB7.0.2.37/28/999'
]
thisua = random.choice(yhdl) #随機擷取代理資訊
headers = ("User-Agent",thisua) #拼接報頭資訊
opener = urllib.request.build_opener() #建立請求對象
opener.addheaders=[headers] #添加報頭到請求對象
urllib.request.install_opener(opener) #将報頭資訊設定為全局,urlopen()方法請求時也會自動添加報頭
#請求
yh_dl() #執行使用者代理池函數
url = 'https://www.qiushibaike.com/'
html = urllib.request.urlopen(url).read().decode("utf-8")
print(html)
這樣爬蟲會随機調用,使用者代理,也就是随機報頭,保證每次報頭資訊不一樣
【轉載自:
http://www.lqkweb.com】