Environment Construction
• Installation of python3.6.3;
• Install selenium module, operate the browser through the interface;
• Installation of pillow, pytesseract, TESSERACT-OCR modules (verification code identification);
Complete Code
Here, using Firefox as a tool, the following code will work only if it is installed on a PC with a Firefox browser.
#-*-Coding:utf-8-*-# @Author: Yangjun # @Date: 2018-01-12 10:26:48 # @Last Modified by:dream # @Last Modified ti ME:2018-01-14 13:45:43 Import requests import pytesseract from PIL import Image to selenium import webdriver from Sel Enium.webdriver.common.keys Import Keys def codedemo (ImagePath): ' indentify verify code ' IM = ImagePath tessdata_dir_config = '--tessdata-dir ' E:\Tesseract-OCR\\tessdata ' Vcode = pytesseract.image_to_string (IM, lang= ' Chi_sim ', config=tessdata_dir_config) return vcode[:6] def SendMessage (SelectCountry, Mobil ENumber, sendmsg): Driver = Webdriver. Firefox () driver.get ("http://www.afreesms.com/freesms/") Try:basepath = '/html/body/div[3]/div/div/field Set/table ' +\ '/tbody/tr[2]/td[2]/table/tbody/tr/td/form/table/tbody ' country = driver.find_element_b Y_xpath (' {}/tr[2]/td[2]/select '. Format (basepath)) Country.send_keys (selectCountry) print (' Set Country success. ') Number = Driver.find_element_by_xpath (' {}/tr[3]/td[2]/input '. Format (basepath)) Number.send_k
Eys (mobilenumber) print (' Set mobilenumber success. ') message = Driver.find_element_by_xpath (' {}/tr[4]/td[2]/textarea '. Format (basepath)) message.s
End_keys (sendmsg) image = driver.find_element_by_id (' captcha ') driver.save_screenshot ('. \screen.png ') left = image.location[' x '] top = image.location[' y '] right = left + image.size[' width '] Bott Om = top + image.size[' height ']-Ten im = Image.open ('. \screen.png ') ImagePath = Im.crop (left, top, right , bottom)) # imagepath.show () key = Codedemo (ImagePath) print (' indentify verify code[{}] complete.
'. Format (key) Indentifycode = Driver.find_element_by_xpath (' {}/tr[6]/td[2]/input '. Format (basepath) ) inDentifycode.send_keys (key) print (' Set Verify code success. ')
# import Pdb;pdb.set_trace () Submit = driver.find_element_by_id (' Submit ') Submit.send_keys (Keys.return) Except Exception as Why:driver.close () errormsg = ' If hasattr (why, ' message '): ER
rormsg = why.message else:errormsg = Why.args print (' send failed. ')
Print (errormsg) print (' Send success. ') Driver.close () if __name__ = = ' __main__ ': selectcountry = ' China ' mobilenumber = ' sendmsg = ' ' Test information:
Hello world. ' # Send message interval (how often, in seconds) sleeptime = 0 # maximum number of times MaxNumber = 1 Sendnumber = 0 while Sendnumbe R < Maxnumber:sendmessage (SelectCountry, Mobilenumber, sendmsg) Sendnumber + + 1 if Sleeptime & Gt 0:time.sleep (sleeptime) print (' All message send complete. ')
This article is reproduced in:
http://blog.csdn.net/freeboy1234/article/details/79296582