Python自动发送短信脚本
2018-02-09 11:20
489 查看
自动发送短信
发现一个免费发送短信平台http://www.afreesms.com/freesms/
所以试着用python selenium模块及验证码识别技术写了一个自动发送脚本。
@特此备注:技术没有善恶,脚本仅用于学习使用。
环境搭建
1、安装python3.6.3;2、安装selenium模块,通过接口操作浏览器;
3、安装pillow、pytesseract、tesseract-ocr模块(验证码识别);
完整代码
这里以火狐浏览器为操作工具,以下代码只有在装有火狐浏览器的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:43 import requests import pytesseract from PIL import Image from selenium import webdriver from selenium.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, 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.')
本人邮箱:
zhao.yj@yahoo.com欢迎留言交流!!
相关文章推荐
- Python 自动发送短信脚本
- Python自动发送邮件的脚本源码
- zabbix发送短信脚本_python实现
- zabbix增加手机短信、邮件监控的注意要点,SSL邮件发送python脚本
- 【Python学习笔记】-自动发送邮件脚本
- Python自动发送邮件的脚本源码
- Oracle AWR 报告 每天自动生成并发送邮箱 Python脚本
- 一个python的邮件发送脚本,自动,定时,可以附件发送,抄送
- python自动发送短信
- Python自动发送邮件的脚本源码
- python自动查询12306余票并发送邮箱提醒脚本
- Oracle AWR 报告 每天自动生成并发送邮箱 Python脚本
- Oracle AWR 报告 每天自动生成并发送邮箱 Python脚本
- python_发送短信脚本
- 一个python的邮件发送脚本,自动,定时,可以附件发送,抄送,附有说明文件
- 【转】Oracle AWR 报告 每天自动生成并发送邮箱 Python脚本(一)
- python实现zabbix发送短信脚本
- 一个python的邮件发送脚本,自动,定时,可以附件发送,抄送,附有说明文件
- 一个python的邮件发送脚本,自动,定时,可以附件发送,抄送,附有说明文件 (本脚本是python的2.7.x环境下可用)