利用Python实现批量注册网站用户,注意不可用于商业用途哦!

场景目标

现在大多数网站的「用户注册功能」都需要用户正确输入了验证码,才能发起注册的请求,如果想大量注册用户,正确识别验证码变的很关键。

普通的验证码使用 tesserocr,加上训练可以完成,如果想简单一点,可以使用「百度云的文字识别 API」。

今天的目标是使用 selenium + 百度云OCR 批量注册「中知网」一批用户。


<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">from aip import AipOcr

""" 你的 APPID AK SK """

APP_ID = ‘1547‘

API_KEY = ‘VBoMZ6XUX11‘

SECRET_KEY = ‘GPvqLVeGIMOR57**‘

client = AipOcr(APP_ID, API_KEY, SECRET_KEY)

</pre>

分析思路

我们打开中国知网的注册页面,检查验证码图片的元素,通过 src 属性可以知道验证码的请求地址是:

http://my.cnki.net/elibregister/CheckCode.aspx

每次刷新页面或者点击验证码图片,都会重新加载一次验证码。这里我们只能使用截取验证码区域保存到本地。

另外,截图验证码图片,需要使用「Phtotoshop」来获取验证码的左上角和右下角的坐标数据。

<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">def get_code(self):

1.截图并保存到本地

self.driver.get_screenshot_as_file(‘./%s‘ % self.screen_shot_file_name)

2.打开文件

screenshot_image = Image.open(‘./%s‘ % self.screen_shot_file_name)

3.设置要裁剪的区域(验证码所在的区域)

code_box = (899, 819, 1048, 883)

4.截图:生成只有验证码的图片

code_image = screenshot_image.crop(code_box)

5.保存到本地

code_image.save("./%s" % self.code_file_name)

6.以byte读取图片

image = get_file_content("./%s" % self.code_file_name)

7.使用百度OCR识别验证码

result = client.basicAccurate(image)

print(result)

识别的文字内容

word_result = result.get(‘words_result‘)[0].get(‘words‘)

return word_result

</pre>

然后使用 Image 类的 crop() 函数截取验证码图片并保存到本地,接着可以调用百度云 OCR API 去识别验证码。

另外,由于验证码识别率有一定几率的失败,需要循环去填入验证码,然后点击外侧容器元素操作,直到验证码识别正确。

<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">while True:

code = self.get_code().strip()

error_tips_element = self.driver.find_element_by_id(‘span_oldcheckcode‘)

print(‘验证码为:%s‘ % code)

code_input_element.clear()

code_input_element.click()

code_input_element.send_keys(code)

点击外围的容器,判断验证码是否输入正确

container_element.click()

显示了错误信息:验证码输入错误

if error_tips_element.text:

time.sleep(2)

print(‘验证码验证失败,点击验证码图片‘)

点击验证码图片,重新加载验证码

code_img_element.click()

continue

else:

print(‘验证码验证成功‘)

break

</pre>

现在就可以通过 webdriver 获取到其他输入框元素,填充用户名、密码、邮箱地址,点击注册按钮,实现注册一个用户的功能。

多次循环上面的操作,就可以实现批量注册的需求。

<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">def register(self, code):

用户名输入框

username_input_element = self.driver.find_element_by_id(‘username‘)

密码输入框

password_input_element = self.driver.find_element_by_id(‘txtPassword‘)

邮箱输入框

txtEmail_input_element = self.driver.find_element_by_id(‘txtEmail‘)

注册按钮

submit_btn_element = self.driver.find_element_by_id(‘ButtonRegister‘)

username_input_element.send_keys(self.username)

password_input_element.send_keys(self.password)

txtEmail_input_element.send_keys(self.email)

submit_btn_element.click()

</pre>

注:不能用于商业用途,批量注册然后在贩卖号哦!

最后,如果你跟我一样都喜欢python,想成为一名优秀的程序员,也在学习python的道路上奔跑,欢迎你加入python学习群:839383765 群内每天都会分享最新业内资料,分享python免费课程,共同交流学习,让学习变(编)成(程)一种习惯!

原文地址:http://blog.51cto.com/14186420/2347748

时间: 2024-07-31 08:21:00

利用Python实现批量注册网站用户,注意不可用于商业用途哦!的相关文章

利用Python实现批量下载腾讯视频!

导语 利用Python下载腾讯非VIP视频,也就是可以免费观看的视频.做这个的起因是最近在看一个叫"请吃红小豆吧"的动漫,一共三分钟的动漫,广告时间竟然要一分钟,实在忍无可忍,于是干脆写个脚本把动漫都先下载下来再看,顺便过来分享一波,让我们愉快地开始吧~ 开发工具 Python版本:3.6.4 相关模块: PIL模块: requests模块: click模块: 以及一些Python自带的模块. 环境搭建 安装Python并添加到环境变量,pip安装需要的相关模块即可. 原理简介 做之前

利用 Python 进行批量更改文件后缀

利用 Python 进行批量更改文件后缀 代码 import os files = os.listdir('.') for file_name in files: portion = os.path.splitext(file_name) if portion[1] == ".jpg": new_name = portion[0] + ".gif" os.rename(file_name, new_name) 以上代码为将后缀为 "jpg" 的文

利用python爬下段子网站的搞笑段子

众所周知,python是写爬虫的利器,今天作者用python写一个小爬虫爬下一个段子网站的众多段子. 目标段子网站为“http://ishuo.cn/”,我们先分析其下段子的所在子页的url特点,可以轻易发现发现为“http://ishuo.cn/subject/”+数字, 经过测试发现,该网站的反扒机制薄弱,可以轻易地爬遍其所有站点. 现在利用python的re及urllib库将其所有段子扒下 import sys import re import urllib #返回html格式 def g

自动化测试——利用Python+Selenium批量录入测试数据

背景:测试过程中,为实现将不同的测试数据批量循环录入,考虑需对已通过读取csv文件方式参数化的脚步进一步地参数化,示例代码如下: import csv...data_set = r'C:\\test.csv'data = csv.reader(file(data_set,'rb')) def test_insert(self):    ...    # 每条待录入的数据共四个字段    info0 = 0    info1 = 1    info2 = 2    info3 = 3    for

一步步分析百度音乐的播放地址,利用Python爬虫批量下载

百度音乐不需要登录也可以下载?听到这个消息是不是很兴奋呢, 接下来我们打开百度音乐,随便打开一首歌,切换到百度播放页面:如图 我这里用的是Firfox 浏览器,打开firebug 先清空所有的请求,如图: 现在我们重新刷新下页面,看到这个.mp3的地址就是百度音乐的地址,我们可以直接复制到迅雷里下载,但是这种做法太初级了吧,如果有很多首歌曲呢,每个都这样复制,岂不是很麻烦啊.,接下来我们继续分析. 这个链接有个特点,就是music/1658513  这个是什么呢? 你猜的没错,这个是每首歌曲的I

python爬虫,一段完整的python爬虫批量下载网站图片资源的代码

# 本程序为爬虫学习代码,成功爬取了漫微网站上的全部图片内容 import re import os import requests def getHTMLText(url): try: r=requests.get(url) r.raise_for_status() r.encoding=r.apparent_encoding return r.text except: print("request failed") url = 'http://marvel.mtime.com/'

#IT明星不是梦#利用Python进行网站日志分析

网站的访问日志是一个非常重要的文件,通过分析访问日志,能够挖掘出很多有价值的信息.本文介绍如何利用Python对一个真实网站的访问日志进行分析,文中将综合运用Python文件操作.字符串处理.列表.集合.字典等相关知识点.本文所用的访问日志access_log来自我个人的云服务器,大家可以从文末的附件中下载. 1.提取指定日期的日志 下面是一条典型的网站访问日志,客户端访问网站中的每个资源都会产生一条日志. 193.112.9.107 - - [25/Jan/2020:06:32:58 +080

使用Python解决Teamviewer被误认为商业用途的问题

Teamviewer是一款非常酷的远程控制系统,可以远程协同工作,分为个人版本和商业版本.个人版本可以基于非商业目的自由使用,商业版本需要付费(至少一个月49美元). Teamviewer会根据一套算法检测当前是否用于商业用途,可能根据在线时间,是否跨网段远程访问,当前操作系统是否为专业或企业版,以及其他因素进行检测,具体算法不得而知,不过有时明明只是个人非盈利目的使用,却弹出下面的对话框,在1到5分钟后就会自动断开,然后就连不上了,触发重新运行Teamviewer. 其实解决这个问题也很简单,

利用Python批量保存51CTO博客

一.背景 最近在整理博客,近在51CTO官网存在文章,想将之前写的全部保存到本地,发现用markdown写的可以导出,富文本的则不行,就想利用Python批量保存自己的博客到本地. 二.代码 git地址 #!/bin/env python # -*- coding:utf-8 -*- # _auth:kaliarch import requests import time from bs4 import BeautifulSoup from selenium import webdriver c