python实现手机号获取短信验证码 | 对接打码平台

前言

本篇文章主要讲了如何利用python自动注册网站(自动识别验证码)
在这里插入图片描述

ID在用户中心最下面的软件ID里获取
识别类型在“价格体系”中查看
首先需要花1块钱到超级鹰打码平台上买点题分(1块钱1000提分,可以打100次)
超级鹰官网http://www.chaojiying.com

短信获取.py

import time
import requests
from chaojiying import Chaojiying_Client #调用chaojiying.py文件中的函数

"""如果请求的网站需要时间戳,则使用以下代码获取,不需要则忽略"""
def get_time():
    """时间戳的获取"""
    now_time=str(int(time.time()*1000))
    #print("当前的时间戳为:",now_time)
    return now_time
    
#准备请求参数
headers={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:94.0) Gecko/20100101 Firefox/94.0'}
time_one=get_time()

#发送图片验证码请求
img_url='https://my.guidechem.com/captcha/captcha-image.action?random='+ time_one 
response=requests.get(url=img_url,headers=headers)#获取验证码图片
img_data=response.content  #提取二进制数据
#保存验证码图片
with open('yzm.jpg',mode='wb') as f: #b表示二进制
    f.write(img_data)
    
# 识别验证码(对接打码平台)
code=Chaojiying_Client('超级鹰用户名', '超级鹰密码', '超级鹰id').run() #这里的run就是在chaojiyin.py中自定的函数
print('当前验证码结果为',code)
#两次请求要保证是同一个用户cookie<用户的身份标识>
cookieJar=response.cookies
cookies=cookieJar.get_dict()  #把cookieJar转换字典
#发送短信验证的请求参数,这里的post参数是根据网站情况而变的
data={
    'mobileNo': 手机号,
    'captcha': code
    }
# 请求发送验证码
code_url='https://my.guidechem.com/sms/sendRegCode.action'
response_2=requests.post(url=code_url,headers=headers,data=data,cookies=cookies)
print(response_2.json()) #打印返回值,查看是否发送成功

chaojiying.py(超级鹰接口文件)

获取途径,http://www.chaojiying.com
在这里插入图片描述
在这里插入图片描述

chaojiying操作过程如下:

1.在下载后的py文件中新定义一个函数run

    def run(self):
        chaojiying = Chaojiying_Client('账号', '密码', 'ID')	
        im = open('yzm.jpg', 'rb').read()
        result=chaojiying.PostPic(im,1004)#1004为识别类型
        return result['pic_str']

2.修改下面的内容,yzm.jpg是本地的验证码图片文件

if __name__ == '__main__':
    chaojiying = Chaojiying_Client('账号', '密码', 'ID')		#用户中心>>软件ID 生成一个替换 96001
    im = open('yzm.jpg', 'rb').read()													#本地图片文件路径 来替换 a.jpg 有时WIN系统须要//
    result=chaojiying.PostPic(im,1004)
    print(result['pic_str'])	

其他细节省略…


最后chaojiying.py文件内容如下:

#!/usr/bin/env python
# coding:utf-8

import requests
from hashlib import md5

class Chaojiying_Client(object):

    def __init__(self, username, password, soft_id):
        self.username = username
        password =  password.encode('utf8')
        self.password = md5(password).hexdigest()
        self.soft_id = soft_id
        self.base_params = {
            'user': self.username,
            'pass2': self.password,
            'softid': self.soft_id,
        }
        self.headers = {
            'Connection': 'Keep-Alive',
            'User-Agent': 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)',
        }

    def PostPic(self, im, codetype):
        """
        im: 图片字节
        codetype: 题目类型 参考 http://www.chaojiying.com/price.html
        """
        params = {
            'codetype': codetype,
        }
        params.update(self.base_params)
        files = {'userfile': ('ccc.jpg', im)}
        r = requests.post('http://upload.chaojiying.net/Upload/Processing.php', data=params, files=files, headers=self.headers)
        return r.json()

    def ReportError(self, im_id):
        """
        im_id:报错题目的图片ID
        """
        params = {
            'id': im_id,
        }
        params.update(self.base_params)
        r = requests.post('http://upload.chaojiying.net/Upload/ReportError.php', data=params, headers=self.headers)
        return r.json()
    def run(self):
        chaojiying = Chaojiying_Client('账号', '密码', 'ID')		#用户中心>>软件ID 生成一个替换 96001
        im = open('yzm.jpg', 'rb').read()													#本地图片文件路径 来替换 a.jpg 有时WIN系统须要//
        result=chaojiying.PostPic(im,1004)
        return result['pic_str']


if __name__ == '__main__':
    chaojiying = Chaojiying_Client('账号', '密码', 'ID')		#用户中心>>软件ID 生成一个替换 96001
    im = open('yzm.jpg', 'rb').read()													#本地图片文件路径 来替换 a.jpg 有时WIN系统须要//
    result=chaojiying.PostPic(im,1004)
    print(result['pic_str'])												#1902 验证码类型  官方网站>>价格体系 3.4+版 print 后要加()


最后运行"短信获取.py"执行结果:
在这里插入图片描述

  • 1
    点赞
  • 47
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

向阳-Y.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值