Python 自動傳送簡訊指令碼__Python

來源:互聯網
上載者:User

環境搭建

   ·安裝python3.6.3;

   ·安裝selenium模組,通過介面操作瀏覽器;

   ·安裝pillow、pytesseract、tesseract-ocr模組(驗證碼識別);

完整代碼

   這裡以Firefox瀏覽器為操作工具,以下代碼只有在裝有Firefox瀏覽器的pc上才會正常運行。

# -*- coding: utf-8 -*-# @Author: yangjun# @Date:   2018-01-12 10:26:48# @Last Modified by:   dream# @Last Modified time: 2018-01-14 13:45:43import requestsimport pytesseractfrom PIL import Imagefrom selenium import webdriverfrom selenium.webdriver.common.keys import Keysdef 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, mobileNumber, sendMsg):    driver = webdriver.Firefox()    driver.get("http://www.afreesms.com/freesms/")    try:        basePath = '/html/body/div[3]/div/div/fieldset/table' +\            '/tbody/tr[2]/td[2]/table/tbody/tr/td/form/table/tbody'        country = driver.find_element_by_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_keys(mobileNumber)        print('Set mobileNumber success.')        message = driver.find_element_by_xpath(            '{}/tr[4]/td[2]/textarea'.format(basePath)        )        message.send_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']        bottom = top + image.size['height'] - 10        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'):            errorMsg = why.message        else:            errorMsg = why.args        print('send failed.')        print(errorMsg)    print('send success.')    driver.close()if __name__ == '__main__':    selectCountry = '中國'    mobileNumber = ''    sendMsg = '''        測試資訊:hello world.    '''    # 發送資訊間隔時間(多久發一次,單位: 秒)    sleepTime = 0    # 最多發送多少次    maxNumber = 1    sendNumber = 0    while sendNumber < maxNumber:        sendMessage(selectCountry, mobileNumber, sendMsg)        sendNumber += 1        if sleepTime > 0:            time.sleep(sleepTime)    print('All message send complete.')

本文轉載於:

   http://blog.csdn.net/freeboy1234/article/details/79296582

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.