python結(jié)合selenium獲取XX省交通違章數(shù)據(jù)的實現(xiàn)思路及代碼
前言:
目前在研究易信公眾號,想給公眾號增加一個獲取個人交通違章的查詢菜單,通過點擊返回查詢數(shù)據(jù)。以下是實施過程。
一、首先,用火狐瀏覽器打開XX省交管網(wǎng),分析頁面信息:
可以看到共有4種查詢種類,我只要查詢違章數(shù)據(jù),所以分析第一個電子警察信息查詢就好了,用firebug分別查看車牌號碼、車輛識別碼、驗證碼輸入框,可以得到id屬性,分別為:carNum1、carAuthCode1、captcha1。
到這里,我們可以用selenium根據(jù)獲取的id,自動填入車牌號碼、車輛識別碼、驗證碼,但驗證碼如何獲取呢?。
二、獲取驗證碼
第一次、通過Tesseract識別
經(jīng)過測試,識別率太低了,不可行。
第二次、通過cookies查找驗證碼
通過查看服務(wù)器返回的cookies,發(fā)現(xiàn)里面竟然有驗證碼。。。
三、編寫程序測試
1、流程圖和測試結(jié)果
2、源代碼
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.common.keys import Keys from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC class JTWZ(): def __init__(self,carAuthCode,carNum): """ carAuthCode:車輛識別碼 carNum:車牌號 """ self.driver = webdriver.Chrome() self.url = 'http://xxcx.hbsjg.gov.cn:8087/hbjj/' self.carAuthCode=carAuthCode self.carNum=carNum def get_content(self): self.driver.get(self.url) try: element = WebDriverWait(self.driver, 10).until(EC.presence_of_element_located((By.ID, "checkCode1"))) print(u'開始登錄...') except Exception as e: print(e) self.carNum1 = self.driver.find_element_by_id('carNum1') self.carNum1.send_keys(self.carNum) self.carAuthCode1 = self.driver.find_element_by_id('carAuthCode1') self.carAuthCode1.send_keys(self.carAuthCode) captcha1=self.driver.find_element_by_id('captcha1') #從cookies找尋驗證碼 for n in self.driver.get_cookies(): if n.get('name')!=None and n['name']=='RANDOMVALIDATECODEKEY1': checkCode1=n['value'] captcha1.send_keys(checkCode1) sub=self.driver.find_element_by_xpath("http://input[@value='開始查詢']") sub.click() try: element = WebDriverWait(self.driver, 10).until(EC.presence_of_element_located((By.CLASS_NAME, "fsmiddle"))) print(u'獲取違章內(nèi)容成功,保存為:wz.jpg...') self.driver.save_screenshot('wz.jpg') return 0 except: print(u'獲取失敗...') return 1 finally: self.driver.quit() if __name__ == '__main__': jtwz=JTWZ(carAuthCode=000,carNum='') jtwz.get_content()
- Python中使用 Selenium 實現(xiàn)網(wǎng)頁截圖實例
- Python selenium 三種等待方式詳解(必會)
- Python selenium 三種等待方式解讀
- 玩轉(zhuǎn)python selenium鼠標鍵盤操作(ActionChains)
- Python selenium文件上傳方法匯總
- Python selenium 父子、兄弟、相鄰節(jié)點定位方式詳解
- 詳解Python多線程Selenium跨瀏覽器測試
- Python中selenium實現(xiàn)文件上傳所有方法整理總結(jié)
- Python+Selenium自動化實現(xiàn)分頁(pagination)處理
- python+selenium開發(fā)環(huán)境搭建圖文教程
相關(guān)文章
Python?數(shù)據(jù)分析教程探索性數(shù)據(jù)分析
這篇文章主要介紹了Python?數(shù)據(jù)分析教程探索性數(shù)據(jù)分析,文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下2022-08-08Python裝飾器限制函數(shù)運行時間超時則退出執(zhí)行
今天小編就為大家分享一篇關(guān)于Python裝飾器限制函數(shù)運行時間超時則退出執(zhí)行,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-04-04