爬虫和反爬虫
一、什么是反爬蟲
反爬蟲:限制爬蟲程序訪問服務器資源和獲取數據的行為
限制手段:請求限制,拒絕響應,客戶端身份驗證,文本混淆和使用動態渲染技術等
二、反爬蟲的分類:
身份識別發爬蟲
? ? ? ? 驗證請求頭信息、驗證請求參數,使用驗證碼等
爬蟲行為反爬蟲:
? ? ? ? 對ip進行限制,使用蜜罐獲取ip,假數據等
數據加密反爬蟲:
? ? ? ? 自定義字體,數據圖片,編碼格式等
身份識別發爬蟲解決思路
Header反爬,通過User-agent字段
???????????????????????通過cookie字段
? ? ? ? ? ? ? ? ? ? ? ?通過referer字段
基于請求參數反爬
? ? ? ? ? ? ? ? ? ? ? ? 仔細分析抓到的包,搞清楚請求之間的聯系
驗證碼反爬:
? ? ? ? ? ? ? ? ? ? ? ? Pytesseract/商業打碼平臺
2.1 驗證碼的處理和識別
圖片識別引擎:
ocr是指使用掃描儀或者數碼相機對文本資料進行掃描成圖像文件,然后對圖像文件進行分析處理,自動識別獲取文字信息及版面信息的軟件
Tesseract? 開源免費
下載地址:Index of /tesseract
調用圖片識別引擎
安裝pil和pytesseract?
pip install pillow? # 一個python圖像處理庫,pytesseract依賴pip install pytesseract from PIL import Image import pytesseract# 打開圖片 img = Image.open('img/02.jpeg')# 查看圖片 # img.show()# 調用引擎進行識別 pytesseract.pytesseract.tesseract_cmd=r'D:\Program Files (x86)\tesseract\tesseract.exe' text = pytesseract.image_to_string('img/01.jpeg') print(text)點選式的驗證碼識別
識別網站 http://121.41.201.214:8083/#/useOnline/pointFixed復雜的圖片可以使用超級鷹進行識別
# http://121.41.201.214:8083/#/useOnline/pointFixed # 使用selenium打開網站 # 截取全屏圖片 # 獲取驗證碼的區域,獲取驗證碼控件 # 截取驗證碼圖片 # 將驗證碼圖片發送給超級鷹 # 根據超級鷹返回的文字左表執行點擊操作 import time from PIL import Image from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait # 等待 from selenium.webdriver.support import expected_conditions from selenium.webdriver.support import expected_conditions as EC # 監控控件 from 爬蟲開發.chaojiying.chaojiying import Chaojiying_Client from selenium.webdriver import ActionChains # 點擊class Click(object):def __init__(self):self.driver = webdriver.Chrome()# 瀏覽器窗口最大化self.driver.maximize_window()def handle_captcha(self):"""打開頁面"""self.driver.get('http://121.41.201.214:8083/#/useOnline/pointFixed')# 一般電腦會把電腦界面顯示比例設置位125% 這里將其改回來self.driver.execute_script('document.body.style.zoom="0.8"')if WebDriverWait(self.driver, 5, 0.5).until(EC.presence_of_element_located((By.CLASS_NAME, "verify-img-panel"))):captcha_element = self.save()time.sleep(2)if captcha_element:nodes = self.handle_chaojiying()if nodes:print('驗證碼識別成功開始點擊驗證碼')for i in nodes.split('|'):ActionChains(self.driver).move_to_element_with_offset\(captcha_element,int(i.split(',')[0]),int(i.split(',')[1])).click().perform()time.sleep(1)self.driver.quit()def handle_chaojiying(self):"""識別驗證碼"""chaojiying = Chaojiying_Client('超級鷹賬號', '123456', 'id') # 用戶中心>>軟件ID 生成一個替換 96001with open('img/chptcha.png', mode='rb') as f:img = f.read()chptcha_data = chaojiying.PostPic(img, 9103).get('pic_str')print(chptcha_data)return chptcha_datadef save(self):"""截取圖片,保存圖片"""# 截取全屏圖片self.driver.save_screenshot('img/browser.png')# 找到驗證碼圖片控件captcha_element = self.driver.find_element(by=By.XPATH, value='//div/img')# 獲取驗證碼左上角坐標location = captcha_element.location# 獲取驗證碼的大小,寬和高size = captcha_element.size# 驗證碼尺寸code = (location.get('x'), location.get('y'), location.get('x') + size.get('width'),location.get('y') + size.get('height') + 50)img = Image.open('img/browser.png')captcha = img.crop(code)captcha.save('img/chptcha.png')return captcha_elementif __name__ == '__main__':c = Click()c.handle_captcha()三、基于爬蟲行為反爬和解決思路
通過請求ip/賬號單位時間內請求頻率,次數反爬
? ? ? ? 使用ip代理,多個賬號反反爬
通過同一ip/賬號請求間隔進行反爬
? ? ? ? 使用ip代理,設置隨機休眠進行反反爬
通過js實現跳轉反爬
? ? ? ? 多次抓包,分析規律
通過蜜罐(陷阱)捕獲ip
? ? ? ? 完成爬蟲之后,測試爬取、仔細分析響應內容,找出陷阱
通過假數據進行反爬
? ? ? ? 長期運行,對比數據庫中數據同實際頁面數據
阻塞任務隊列
? ? ? ? 分析獲取垃圾url的規律,對url進行過濾
阻塞網絡IO反爬
? ? ? ? 審查抓取連接,對請求時間計時
四、基于數據加密反爬和解決思路
自定義字體反爬
? ? ? ? 切換到手機版/解析自定義字體
通過js動態生成數據進行反爬
? ? ? ? 分析js生成數據的流程,模擬生成數據
通過數據圖片化進行反爬
? ? ? ? 通過使用圖片引擎,解析圖片數據
通過編碼格式進行反爬
? ? ? ? 測試不同格式解碼,獲取正確的解碼格式
總結
- 上一篇: 关于使用jQuery前端上传文件
- 下一篇: 使用HBuilder X 创建项目,(u