First, prepare
1, environmental infrastructure: Python3 + pycharm + selenium + request + Pillow, drivers chrome browser.
1) python: here is Python3.7, installation slightly;
2) pycharm: Editor to create Python engineering, installation slightly;
3) selenium: Python3 environment should own pip.exe, simply enter pip install selenium cmd command window for installation;
4) request: identifying an interface, simply enter pip install request for installation cmd command window;
5) Pillow: Images for cutting, simply enter pip install pillow cmd command window for installation;
Driver 6) chrome browser: to: http://chromedriver.storage.googleapis.com/index.html? Download the appropriate chromedriver.exe, backward compatible with 64-bit, you can download 32. On the Python installation directory, see https://www.cnblogs.com/yuntimer/p/11178530.html after decompression
2, easy to source website - Interface algorithms (fee), where the need to register the purchase verification algorithm code interfaces, users prepare the above mentioned id ( my_appId ) and password ( my_appSecret ), the site where users note.
Second, the realization
1, the source site Yi - authentication code recognition - Request example, download the Python-SDK, unzip the Python installation on libraries (eg: D: \ Python37 \ Lib);
2, the use of selenium start the chrome browser, enter verification code to fill in the page:
1 '' ' call the chrome browser, open maximized browser ' '' 2 Driver = webdriver.Chrome () # open the browser 3 driver.get ( " Code page URL " ) # to open the address 4 driver.maximize_window () . 5 the time.sleep (2)
3, the screenshot browser images, cut (Pillow library), written picture cutting function:
1 '' ' 2, screenshot browser images, cut (Pillow library), extracted from the CAPTCHA page ' '' 2 DEF jietu (): . 3 driver.save_screenshot ( " D: /yemian.png " ) 4 '' ' using a coordinate manner, the coordinates of the upper left corner to get the codes '' ' . 5 LEFT_TOP = driver.find_element_by_id ( " captchaImg " ) .location . 6 Print ( " left corner " , LEFT_TOP) . 7 X1 = LEFT_TOP [ ' X ' ] # verification box x coordinate of the upper left corner . 8 Y1 = LEFT_TOP [ 'and '] # Verification box y-axis coordinates of the upper left corner 9 '' ' taken bottom right coordinates ' '' 10 IMG = driver.find_element_by_id ( " captchaImg " ) # positioning image . 11 X2 = img.size [ ' width ' ] X1 + # Get the width of the image + x1 ===== "right scale 12 is Y2 = img.size [ ' height ' ] + Y1 # coordinates acquired image height Y1 ================ +" 13 is Print ( " four points coordinates " , X1, Y1, X2, Y2) 14 '' ' by using the four image coordinates cut ' '' 15 yan_img Image.open = (" D: /yemian.png " ) # Open Image 16 yan_ma = yan_img.crop ((X1, Y1, X2, Y2)) . 17 yan_ma.save ( " D: /yanzhengma.png " ) # saved on the hard disk
4, for the identification codes, (Yi source interface), the source site Yi - image verification method using a corresponding identification code:
Easy source site Example:
from ShowapiRequest import ShowapiRequest r = ShowapiRequest("http://route.showapi.com/184-4","my_appId","my_appSecret" ) r.addFilePara("image", "替换为你的文件") r.addBodyPara("typeId", "34") r.addBodyPara("convert_to_jpg", "0") r.addBodyPara("needMorePrecise", "0") res = r.post() print(res.text) # 返回信息
Self-write implementation:
'' 'For further verification code identification (easy to source interfaces)' '' DEF shibieyanzhengma (): # environment using an interface to access interface address ===> you've got to pay attention to things yun1 = ShowapiRequest ( "http: //route.showapi .com / 184-4 "," my_appId "," my_appSecret ") # increase the parameter request to an interface yun1.addBodyPara (" typeId "," 36 ") #" 36 ": combinations of alphanumeric, 6 codes yun1.addBodyPara ( "convert_to_jpg", "0" ) # "0": do not need to convert the picture # tell interface identification verification code image file yun1.addFilePara ( "Image", "D: /yanzhengma.png") # access interface result = yun1 .post (). JSON () Print (Result) # extract valid data from JSON text Result = [ 'showapi_res_body'] [ 'the Result'] Print ( "verification code is", text) return text
Third, script
NOTE: The above implementation is written alphanumeric identification codes _ file type codes.
1, the identification verification - alphanumeric composition 6:
pending upgrade