Foreword:
I am currently studying the Yixin official account. I want to add a query menu for obtaining personal traffic violations to the official account, and return the query data by clicking on it. The following is the implementation process.
1. First, use Firefox to open the XX Provincial Traffic Management Network and analyze the page information:
You can see that there are 4 types of queries. I only need to query violation data, so I just analyze the first electronic police information query. Use firebug to check the license plate number, vehicle identification code, and verification code input box respectively, and you can get the ID Properties, respectively: carNum1, carAuthCode1, captcha1.
At this point, we can use selenium to automatically fill in the license plate number, vehicle identification code, and verification code based on the obtained ID. But how to obtain the verification code? .
2. Obtain verification code
First time, recognized by Tesseract
After testing, the recognition rate is too low and is not feasible.
The second time, find the verification code through cookies
By checking the cookies returned by the server, I found that there was a verification code in them. . .
3. Write program test
1. Flowchart and test results
2. Source code
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("//input[@value='开始查询']") sub.click() try: element = WebDriverWait(self.driver, 10).until(EC.presence_of_element_located((By.CLASS_NAME, "fsmiddle"))) print(u'获取违章内容成功,保存为: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()