Use Google Chrome to automatically log in to the web page, take a screenshot of the page, locate the verification code, and use Baidu text recognition to obtain the verification code to log in
Download the chromedriver. Exe and to project directory, choose corresponding version sites.google.com/a/chromium. Google browsing…
1. Third-party extension libraries must be installed before use
PIP install Selenium install Image PIP install Bidu -aip PIP install PyInstallerCopy the code
2. Encapsulate Baidu cloud interface as Baidu.py
#! /usr/bin/python # -*- coding: Utf-8 - * - from aip import AipOcr from aip import AipImageClassify "' baidu cloud: https://console.bce.baidu.com/ http://ai.baidu.com/ai-doc/OCR/3k3h7yeqa "" APP_ID = 'baidu cloud access' API_KEY = 'Baidu cloud access' SECRET_KEY = 'Baidu cloud access' # text recognition class word: # {'log_id': 4411569300744191453, 'words_result_num': 1, 'words_result': [{'words': ' Y7Y.: 4'}]} def __init__(self, filePath='', options='', url='', idCardSide='front'): Self. client = AipOcr(APP_ID, API_KEY, SECRET_KEY) self.client = AipOcr(APP_ID, API_KEY, SECRET_KEY) Self. url = URL # Local image data, base64 encoding, required base64 encoding size is not more than 4M, the shortest edge at least 15px, Self. image = "# self.options = options # front: id side with photo; Self. idCardSide = idCardSide if filePath: # with open(filePath, 'rb') as fp: self.image = fp.read() def General(self): Return self.client.basicGeneral(self.image, self.options) # Def GeneralUrl(self) = def GeneralUrl(self); "" "calls the general character recognition, image parameters for remote url 500 times/day "free" "return self. Client. BasicGeneralUrl (self. The url, self.options) def GeneralLocation(self): """ return self.client.general(self.url, self.options) def GeneralUrlLocation(self): "" return self.client.generalurl (self.url, self.options) def Accurate(self): "" "calls the general character recognition (high precision edition) 500 times/day "free" "return self. The client. The basicAccurate (self image, self. The options) def AccurateLocation (self) : Accurate (self.image, self.options) def EnhancedGeneral(self): "" "calls the general character recognition (including rarely used word version), image parameter for the local image "" "return self. Client. EnhancedGeneral (self image, self.options) def EnhancedGeneralUrl(self): "" "calls the general character recognition (including rarely used word version), image parameters for remote url "" "return self. The client. The enhancedGeneralUrl (self image, self. The options) def Idcard (self) : """ Call id identification, picture parameter is the local picture front: ID contains the photo side; Return self.client.idcard(self.image, self.idcardside, self.options) def Bankcard(self): Return self.client.bankcard(self.image) def DrivingLicense(self): "" "call the driver's license recognition "" "return self. The client. The drivingLicense (self image, self. The options) def VehicleLicense (self) : "" "call vehicle license recognition "" "return self. Client. VehicleLicense (self image, self. The options) def LicensePlate (self) : Return self.client.licenseplate (self.image, self.options) def BusinessLicense(self): self.client.licenseplate (self.image, self.options) def BusinessLicense(self): "" "call the business license recognition "" "return to the self. The client. The businessLicense (self image, self. The options) def Receipt (self) : Return self.client.idcard(self.image, self.options) # def __init__(self, filePath='', options='', url=''): Self. client = AipImageClassify(APP_ID, API_KEY, SECRET_KEY) Self. url = url # Local image data, base64 encoding, request base64 encoding size is not more than 4M, Self. image = "# self.options = options if filePath: self.options = options if filePath: # def Recognition(self) with open(filePath, 'rb') as fp: self.image = fp.read() def Recognition(self): "" "calls the general object recognition 500 times/day "free" "return self. Client. AdvancedGeneral (self image, self. The options)Copy the code
3. Create login.py
#! /usr/bin/python # -*- coding: Utf-8 -*- # PIP install Selenium # PIP install bidu -aip # PIP install pyinstaller Import OS import re import time import Baidu from # pyInstaller -f login.py Selenium import webdriver from PIL import Image I = 0 # CodeImage = path + "/image/code. PNG "# Base_URL = 'link address' browser = webdriver.chrome () browser.maximize_window() browser.implICITly_wait (10) browser.get(base_URL) # (2) Enter the password browser.find_element_by_id("username").send_keys("admin") browser.find_element_by_id("password").send_keys("admin") # Def code_jt(): # (3) Get the image verification code coordinates code_ele = browser.find_element_by_xpath('//img[@style="cursor:pointer;"] ') # print(" code_ele. Location ", code_ele. ", Left = code_ele. Location ['x'] # x coordinates top = code_ele. Location ['y'] # y coordinates right = code_ele Code_ele. Size ['width'] + left # down = code_ele. Size ['height'] + top # image = image.open (loginImage) Code_image = image.crop((left, top, right, Image = image.open (codeImage) img = image.convert('L') # Table = [] for m in range(256): if m < count: table.append(0) else: Table.append (1) img = img.point(table, '1') img.save(codeImage) # def bd_img(I =0): CodeImage = bidu. Word (codeImage) "" results = API.General() # print(results) for results["words_result"]: text = result["words"] text = text.strip() text = re.sub(r'[^A-Za-z0-9]+', '', Re.match ("^[a-za-z0-9]*$", text) and if len(text) == 6: Print (" correct "+ STR (I) +" : "+text) browser.find_element_by_id("code").send_keys(text) Browser.find_element_by_xpath ('//input[@type="submit"]').click() # links = browser.find_element_by_xpath('//a[@class="checkin"]') # links.click() else: Print (" error "+ STR (I) +" : "+ text) browser.find_element_by_xpath('//img[@style="cursor:pointer;"] If I > 10: print(' failed to obtain verification code ') exit(1) else: i += 1 code_jt() bd_img(i) code_jt() bd_img(i)Copy the code
4. Package
In the folder where the py program is located, open CMD, type PyInstaller -f login.py, package as a runnable program, and then use the scheduled tasks of Window, you can implement, scheduled check-in, like and other operations after login.
pyinstaller -D -w -i "D:\wwwroot\python\login\image\favicon.ico" login.py
Copy the code
Struct. error: unpack requires a buffer of 16 bytes
Ico icon online conversion can be: www.ico51.cn/
Pyinstaller parameters
-f: generates a folder with multi-file mode and fast startup. -d: Generates only one file and does not expose other information. The startup is slow. -w: the window mode is packaged and the console is not displayed. -i: follows the icon path as the application icon.Copy the code
Pyinstaller packages Selenium to remove chromedriver black box.
The solution is to modify line 76 of service.py in the Selenium package
D:\wwwroot\python\Lib\site-packages\selenium\webdriver\common\service.py
try: cmd = [self.path] cmd.extend(self.command_line_args()) self.process = subprocess.Popen(cmd, env=self.env, close_fds=platform.system() ! = 'Windows', stdout=self.log_file, stderr=self.log_file, stdin=PIPE)Copy the code
Is amended as:
try: cmd = [self.path] cmd.extend(self.command_line_args()) self.process = subprocess.Popen(cmd, env=self.env, close_fds=platform.system() ! = 'Windows', stdout=self.log_file, stderr=self.log_file, stdin=PIPE, creationflags=134217728)Copy the code