脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - python网络爬虫实现发送短信验证码的方法

python网络爬虫实现发送短信验证码的方法

2021-09-09 00:39jgdabc Python

这篇文章主要介绍了python网络爬虫实现发送短信验证码的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

前言:今天要总结的是如何用程序来实现短信发送功能。但是呢,可能需要我们调用一些api接口,我会详细介绍。都是自己学到的,害怕忘记,所以要总结一下,让写博客成为一种坚持的信仰。废话不多说,我们开始吧!

网络爬虫实现发送短信验证码

在实现我们目标的功能之前,我们要有自己的思路,否则你没有方向,又如何实现自己的代码功能呢?

我们要发送短信,那么我们其实是需要分析的。我们可以去分析一个可以发送短信的网站页面。

我们来到这里如下:

python网络爬虫实现发送短信验证码的方法

可以看到这是一个注册界面,我们在注册时会被要求需要填写手机号码的·,其实还有一栏验证码识别,像这里打开没有,那你就填写几个号码,发送,多刷新几次,就可以了。

不为别人添麻烦,我填写自己的号码。
多次刷新会出现,不过要填写不同的手机号码。你们懂的,我们要看到这个有验证码的界面。

python网络爬虫实现发送短信验证码的方法

我们打开chrome谷歌抓包工具,也就是邮件检查即可。我们点击network直接进行抓包,记得在抓包前最好清除下面出现的一切包。我们要点击验证码,让网页做出反应,然后同步一下,进行抓包。

python网络爬虫实现发送短信验证码的方法

当我们填写手机号以后,我们只要点击那个验证码,然后进行抓包如下

python网络爬虫实现发送短信验证码的方法

看到没有,其实点击同步瞬间只有一个数据包的,在你做其他的动作时,可能会出现其他的数据包,但是与此无关。

我们直接点进去看

python网络爬虫实现发送短信验证码的方法

我们看这个url

 

我们打开这个url看看庐山真面目

留意观察这个time参数,很明显是一个时间戳参数

时间戳参数又是什么概念呢?这里有必要介绍一下

时间戳 : 格林威治时间1970年1月0点0分0秒到目前为止
秒级时间戳:10数字
毫秒级时间戳 :13位数字
微秒级时间戳:16位数字

可以看到这个time参数属于毫秒级别的时间戳的。

我们访问这个见面,每次刷新都会有不同的验证码,返回当前的时间。如果我们要获取当前的验证码,我们需要url,前面的参数都一样,只有time,我们需要获取time时间。如何获取呢。

python中有一个time库,我们导入,来看如何使用。

下面展示一些 内联代码片

?
1
2
3
4
5
6
7
import time
def get_time() :
  " 获取当前的时间戳"
  now_time =str(int(time.time()*1000))#获取毫秒级的时间戳
  print('当前的时间戳',now_time)
  return now_time
get_time() 

来看运行结果

我们目前可以这样去做

python网络爬虫实现发送短信验证码的方法

我们把这个获取到的时间戳参数加入到url中,我们可以实现动态的获取,每次要要获取这个二维码时就需要指定当前的时间time参数,那我们完全可以这样来构造这个url。我们用一个变量来接收获取的时间戳,然后以字符串的形式加入到time后面。
下面展示一些 内联代码片

?
1
2
3
4
5
6
7
8
9
import time
def get_time() :
  " 获取当前的时间戳"
  now_time =str(int(time.time()*1000))#获取毫秒级的时间戳
  print('当前的时间戳',now_time)
  return now_time
time_one = get_time()
img_url = 'https://uc.creditcard.ecitic.com/citiccard/ucweb/newvalicode.do?time='+time_one
print(img_url)

我们来看是否可以获取到相应正确的url

python网络爬虫实现发送短信验证码的方法

我们点入那个蓝色的链接,来看有没有获取到这个验证码图片。
python网络爬虫实现发送短信验证码的方法
测试证明我们完全是对的。

下一步我们要做的是实现代码的访问,获取并保存这个验证码。为什么保存,我们应该知道这点知识。

python网络爬虫实现发送短信验证码的方法

看这三个提交栏,很明显是一个要提交表单的。提交那就需要post,而post请求呢,就是要提交我们的数据,及手机号码和图形验证码。

当我们把数据提交上去以后,我们在手机上就会收到短信验证码。我们以此来实现发送短信验证码的功能。

我们来保存图片验证码
下面展示一些 内联代码片

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import time
import requests
def get_time() :
  " 获取当前的时间戳"
  now_time =str(int(time.time()*1000))#获取毫秒级的时间戳
  print('当前的时间戳',now_time)
  return now_time
time_one = get_time()
img_url = 'https://uc.creditcard.ecitic.com/citiccard/ucweb/newvalicode.do?time='+time_one
print(img_url)
headers = {
  'user-agent' :'mozilla/5.0 (windows nt 10.0; wow64) applewebkit/537.36 (khtml, like gecko) chrome/88.0.4324.150 safari/537.36'
 
}
response = requests.get(url=img_url,headers = headers)
img_data = response.content
with open('yzm.jpg',mode = 'wb') as file :
  file.write(img_data)

python网络爬虫实现发送短信验证码的方法

可以看到在代码编辑器右边已经出现了保存的图片。

下一步我们继续来分析这个手机号码的数据和图片验证码的数据在哪里传入,又是如何实现。

我们输入一个手机号码,然后输入图片验证码,然后点击免费获取。此时再次进行抓包,抓包的方法与上文的第一次抓包方法相同。

我们来看会出现什么样的包。

python网络爬虫实现发送短信验证码的方法

蓝色部分的就是我们寻找的目标包。然后我们如何去做?点击打开查看相应的代码。

python网络爬虫实现发送短信验证码的方法

看到没有post请求,是因该提交表单数据的。我们看看下面的表单数据

这里你会发现有一点不同电话号码是直接的数字,图片验证码就需要你来处理了,因为我们上文保存的验证码是图片,你如何识别到这图片验证码里面额数据,来进行传入呢?这里我们还需要一个网站。

超级鹰,是用来识别验证码的,其实我们还是调用这个接口。

python网络爬虫实现发送短信验证码的方法

我们点击开发文档,我们是用python写的代码。所以我们点击python的图标,来这里来查看我们需要的。

python网络爬虫实现发送短信验证码的方法

在下面找到超级鹰图像识别,然后点击下载。把里面的api接口的py文件导入到你的python编辑器。我这里是用pycharm写的。所以直接将解压出来的python文件拖入pycharm。

python网络爬虫实现发送短信验证码的方法

下面是里面的部分代码。

python网络爬虫实现发送短信验证码的方法

这里面做了小小的修改。我们直接来看这段代码写了什么。
我来告诉大家原始的代码有问题,很低级的问题。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/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()
 
 
if __name__ == '__main__':
    chaojiying = chaojiying_client('超级鹰用户名', '超级鹰用户名的密码', '96001'#用户中心>>软件id 生成一个替换 96001
    im = open('a.jpg', 'rb').read()                                                 #本地图片文件路径 来替换 a.jpg 有时win系统须要//
    print chaojiying.postpic(im, 1902)                                              #1902 验证码类型 官方网站>>价格体系 3.4+版 print 后要加()

上面这个是他的原始接口代码。就很离谱。分块来分析。

?
1
2
3
4
5
6
7
def __init__(self, username, password, soft_id):
   self.username = username
     password = password.encode('utf8')#没有缩进
   self.password = md5(password).hexdigest()
   self.soft_id = soft_id
   .......
   .......

这块的错误在哪呢?我这里特意表明突出,上面的原始代码直接沾到这里并不突出,但是你用编辑器打开会有问题的。

?
1
2
3
4
if __name__ == '__main__':
    chaojiying = chaojiying_client('超级鹰用户名', '超级鹰用户名的密码', '96001'#用户中心>>软件id 生成一个替换 96001
    im = open('a.jpg', 'rb').read()                                                 #本地图片文件路径 来替换 a.jpg 有时win系统须要//
    print chaojiying.postpic(im, 1902#print没有加   ()                                      #1902 验证码类型 官方网站>>价格体系 3.#4+版 print 后要加()

还有一处,在这里,代码格式都没有写对,我这里指出,读者应该可以发现。这里介意读者可以去平台下载这个接口,自己去修改。

好,且不在谈这些,我们继续。我们还是修改部分代码。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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()
 
 
if __name__ == '__main__':
  chaojiying = chaojiying_client('超级鹰用户名', '超级鹰用户名的密码', '96001')    #用户中心>>软件id 生成一个替换 96001
  im = open('a.jpg', 'rb').read()                                                   #本地图片文件路径 来替换 a.jpg 有时win系统须要//
  print chaojiying.postpic(im, 1902)        #这是原始的代码                                        #1902 验证码类型 官方网站>>价格体系 3.4+版 print 后要加()

我们可以在这内部写一个方法,我们待会要调用这个代码接口时,直接调用这个方法。

注意我们在类里面添加这样一部分代码,就是写一个方法

?
1
2
3
4
5
6
def run(self):
  chaojiying = chaojiying_client(constant.user_name, constant.password,
                  913137) # 用户中心>>软件id 生成一个替换 96001
  im = open('yzm.jpg', 'rb').read() # 本地图片文件路径 来替换 a.jpg 有时win系统须要//
  result = chaojiying.postpic(im, 1004) # 1902 验证码类型 官方网站>>价格体系 3.4+版 print 后要加()
  return result['pic_str']

注意分析这个接口代码,里面 chaojiying = chaojiying_client(constant.user_name, constant.password,913137)

在注释里面其实说的已经很清楚了,这里的constant代表我们要导入的py文件,里面包含你的用户名,密码,以及软件id。

im = open(‘yzm.jpg', ‘rb').read() 打开你保存的验证码文件,上面我们已经保存过。
result = chaojiying.postpic(im, 1004) 1004代表你的验证码类型。

用户名和密码你需要注册一下。那么软件id和验证码类型你该如何确定呢?
这是主页,请点击价格体系

python网络爬虫实现发送短信验证码的方法

在下面你可以来判断你的验证码类型了

python网络爬虫实现发送短信验证码的方法

我们这里需要登录进入用户中心

python网络爬虫实现发送短信验证码的方法

进入如下界面

python网络爬虫实现发送短信验证码的方法

往下拉进入软件id

python网络爬虫实现发送短信验证码的方法

进入后点击生成一个软件id,软件名称和软件说明可以随便填写

python网络爬虫实现发送短信验证码的方法

这样我们就可以获得一个软件id 。

这个constant如何编写,很简单,建立一个py文件,里面写入

?
1
2
user_name=' …'
password='… '

然后保存即可。导入py文件到当前路劲,然后import即可。

现在我们来看完整的代码
接口完整修改后的代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import requests
from hashlib import md5
import constant
 
import constant
 
 
class chaojiying_client(object):
 
  def __init__(self, username, password, soft_id):
    self.username = username
    self.password = md5(password.encode('utf8')).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(constant.user_name, constant.password,
                    913137) # 用户中心>>软件id 生成一个替换 96001
    im = open('yzm.jpg', 'rb').read() # 本地图片文件路径 来替换 a.jpg 有时win系统须要//
    result = chaojiying.postpic(im, 1004) # 1902 验证码类型 官方网站>>价格体系 3.4+版 print 后要加()
    return result['pic_str']
 
 
if __name__ == '__main__':
  chaojiying = chaojiying_client(constant.user_name, constant.password, 913137) # 用户中心>>软件id 生成一个替换 96001
  im = open('yzm.jpg', 'rb').read() # 本地图片文件路径 来替换 a.jpg 有时win系统须要//
  result = chaojiying.postpic(im, 1004)
  print(chaojiying.postpic(im,1004))# 1902 验证码类型 官方网站>>价格体系 3.4+版 print 后要加()

主文件代码,从这里执行

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import time
import requests
from chaojiying import chaojiying_client
import constant
def get_time() :
  " 获取当前的时间戳"
  now_time =str(int(time.time()*1000))#获取毫秒级的时间戳
  print('当前的时间戳',now_time)
  return now_time
time_one = get_time()
img_url = 'https://uc.creditcard.ecitic.com/citiccard/ucweb/newvalicode.do?time='+time_one
print(img_url)
headers = {
  'user-agent' :'mozilla/5.0 (windows nt 10.0; wow64) applewebkit/537.36 (khtml, like gecko) chrome/88.0.4324.150 safari/537.36'
 
}
response = requests.get(url=img_url,headers = headers)
img_data = response.content
with open('yzm.jpg',mode = 'wb') as file :
  file.write(img_data)
print(response)
 
#验证码识别
code = chaojiying_client(constant.user_name,constant.password,913137).run()
print('识别出来的验证码为',code)
 
#请求保证同一个用户
cookiejar = response.cookies
cookies = cookiejar.get_dict()
print(cookies)
data = {
  'phone' :19745678397,
  'imgvalidcode' : code,
 
}
time_two = get_time()
code_url = 'https://uc.creditcard.ecitic.com/citiccard/ucweb/getsms.do?&timestamp'+time_two
requests_two = requests.post(url=code_url,data= data,headers=headers,cookies=cookies)
print(requests_two.json())

我们来看运行结果

python网络爬虫实现发送短信验证码的方法

ok,短信发送成功
需要注意的是,如果你发送多次的话,那么会出现提醒你短信发送频率过高的提示。这是服务器的响应。

我们总结一下该程序实现了发送验证码的功能,如果你需要实现发送你想要的文本,那么你需要调用其它的接口。别的就不多说了,毕竟爬虫也需要讲武德。

到此这篇关于python网络爬虫实现发送短信验证码的方法的文章就介绍到这了,更多相关python爬虫发送短信验证码内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/jgdabc/article/details/113932667

延伸 · 阅读

精彩推荐