七、web爬蟲講解2—urllib庫爬蟲—狀態嗎—異常處理—瀏覽器假裝技術、設置用戶代理

百度雲搜索:http://www.lqkweb.com

搜網盤:http://www.swpan.cn

若是爬蟲沒有異常處理,那麼爬行中一旦出現錯誤,程序將崩潰中止工做,有異常處理即便出現錯誤也能繼續執行下去html

1.常見狀態碼python

301:重定向到新的URL,永久性
302:重定向到臨時URL,非永久性
304:請求的資源未更新
400:非法請求
401:請求未經受權
403:禁止訪問
404:沒找到對應頁面
500:服務器內部出現錯誤
501:服務器不支持實現請求所須要的功能web

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

瀏覽器假裝技術dom

不少網站,作了反爬技術,通常在後臺檢測請求頭信息裏是否有User-Agent瀏覽器信息,若是沒有說明不是瀏覽器訪問,就屏蔽了此次請求ide

因此,咱們須要假裝瀏覽器報頭來請求函數

#!/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()方法請求自動報頭,也就是設置用戶代理ui

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)

這樣爬蟲會隨機調用,用戶代理,也就是隨機報頭,保證每次報頭信息不同

相關文章
相關標籤/搜索