#不使用selenium解决文字点击验证码,处理
在同级目录下创建img文件夹,并把下面图片放进去
调用解决验证码代码的main.py
import requests,re,base64
from hyper.contrib import HTTP20Adapter
from chaojiying import main
from img import img_main
url = 'https://www.jianyu360.com/jylab/supsearch/index.html'
# 登录后的cookie
cookie = "selectNum=1; SESSIONID=c432393c01d63048b98572119bccae8e5f75638f; SESSIONID=c432393c01d63048b98572119bccae8e5f75638f; UM_distinctid=1783dc350d6739-0cdde569b60b06-5771031-1fa400-1783dc350d77c1; selectNum=1; Hm_lvt_72331746d85dcac3dac65202d103e5d9=1615943755,1615945011,1615998044,1616030209; CNZZDATA1261815924=2030409669-1615938790-%7C1616037148; Hm_lpvt_72331746d85dcac3dac65202d103e5d9=1616037802; userid_secure=ExBC9djvB6jbQ4A/mUMWg4vSqI4yrRvbA5YnTqcY418LY5gVsAhhDnlXvQUWaYxDfuWfDX583i4pz/k3yJMkha1wSYTp/sdYCpksdxmpsAh6hNrrT0HaRkq9fiyVNi2kVpwuQUGsg4mt1VShhgvw7pbiS9uHZuLfS7RcB7RCuvQtmTZZOp97Y0PxVP26axkkST0NjoMFYnk3w0wA/nUtZ2augNJ8YqzYKjIMCK5tvK149J0Le9zyFBHHJZYN23QjtYpx3swZif9ws7VlC4SVvF1sM/rv984xuQYC5dPF4ad4tIqEZZ7S4eofU7n3cZTV6dfLx4VEqe37h++BHQBWvCoqKjIwMjEtMDMtMTQgMDA6MDA6MDA="
headers = {
# ":authority": "www.jianyu360.com",
# ":method": "GET",
# ":path": "/jylab/supsearch/index.html",
# ":scheme": "https",
"accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
"accept-encoding": "gzip, deflate, br",
"accept-language": "zh-CN,zh;q=0.9,en;q=0.8,ar;q=0.7",
"cache-control": "max-age=0",
"cookie": cookie,
"sec-ch-ua": '"Google Chrome";v="89", "Chromium";v="89", ";Not A Brand";v="99"',
"sec-ch-ua-mobile": '?0',
"sec-fetch-dest": "document",
"sec-fetch-mode": "navigate",
"sec-fetch-site": "none",
"sec-fetch-user": "?1",
"upgrade-insecure-requests": '1',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36',
# 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36',
}
def img(base64_str,code_name):
with open('img/code.png', 'wb') as f:
"""
base64_str: 加密后的图片字符串
code_name: 需要识别的文字
"""
# 图片base64+加字符更改解密
imgdata = base64.b64decode(base64_str[0].replace('+', '+'))
f.write(imgdata) # 保存原始解密后的图片
img_main(code_name) # 调用普通合成处理
def code_testing(url):
while True:
page_text = requests.get(url=url, headers=headers).text
base64_str1 = re.findall(r'base64,(.*?)"', page_text) # 获取图片加密后的字符串
code_name = re.findall(r'<div>请在下图依次点击:<span>(.*?)</span></div>', page_text) # 获取需要识别的图片文字
if base64_str1:
print('被检测了!')
print('解决检测中。。。')
img(base64_str1, code_name)
pic_str = main() # 调用打码平台
data = {
"antiVerifyCheck": pic_str,
"imgw": "331"
}
print(data)
headers_1 = {
":authority": "www.jianyu360.com",
":method": "POST",
":path": "/article/content/ABCY1wIdSkoMysvEn9zcE8zJicCCj1mdmB0Py8wOCE3eFVzcSdUCbA%3D.html",
":scheme": "https",
"accept": "application/json,text/javascript,*/*;q=0.01",
"accept-encoding": "gzip,deflate,br",
"accept-language": "zh-CN,zh;q=0.9,en;q=0.8,ar;q=0.7",
"app": "jyweb",
"content-length": "55",
"content-type":"application/x-www-form-urlencoded;charset=UTF-8",
"cookie": "SESSIONID=c432393c01d63048b98572119bccae8e5f75638f; SESSIONID=c432393c01d63048b98572119bccae8e5f75638f; UM_distinctid=1783dc350d6739-0cdde569b60b06-5771031-1fa400-1783dc350d77c1; selectNum=1; CNZZDATA1261815924=2030409669-1615938790-%7C1616056293; Hm_lvt_72331746d85dcac3dac65202d103e5d9=1616043005,1616043747,1616046098,1616057263; userid_secure=PDXUOCoopf8OtB9XtuStxKRfGzQxXgPvhFyWDpY6/YHRoSR42FrGI7dLV1Qg84VchfUejfUXD1KpV2PWu4I5lMgL/RY+BdyrQhAaypeDNURjYXgK/+tZUDP5Fn18bTdClvAq2wGD6NBgqU3qciPAv34sWst1VxPHuwNGK4k7MLN3T6JZJjkjEHdJO4LFGAOItd3y4pBrBKQeHiVdUZIU5V7rgQSwCfvBl1M6F4cGU0Mhy1hCM0IiGZXrGfdZOaqDtiHApgpUC6IdKp7ghY17dYjdb0WAPAnoMyP8mbOoVZOMn+/PcHuueX+h/AapJNcwY4mfxe9PsYrggZoIZPX57ioqKjIwMjEtMDMtMTQgMDA6MDA6MDA=",
"origin":"https://www.jianyu360.com",
"referer":"https://www.jianyu360.com/article/content/ABCY1wIdSkoMysvEn9zcE8zJicCCj1mdmB0Py8wOCE3eFVzcSdUCbA%3D.html",
"sec-ch-ua":'"Google Chrome";v="89", "Chromium";v="89", ";Not A Brand";v="99"',
"sec-ch-ua-mobile":"?0",
"sec-fetch-dest":"empty",
"sec-fetch-mode":"cors",
"sec-fetch-site":"same-origin",
"user-agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36",
"x-requested-with":"XMLHttpRequest"
}
sessions = requests.session()
sessions.mount("https://www.jianyu360.com", HTTP20Adapter()) # 解决headers报错问题
qwq = sessions.get(url=url, headers=headers_1, data=data)
print(qwq.text) # 验证验证结果
else:
print("通过")
break
url_current = "https://www.jianyu360.com/jylab/supsearch/index.html" # 当前遇到的验证码的url
code_testing(url_current)
代码平台代码:
超级鹰:https://www.chaojiying.com/
# 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')
# password = password.encode('utf8')
self.password = md5(password).hexdigest()
# self.soft+id = soft_id
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 transdform_code_img(img_path,img_type):
chaojiying = Chaojiying_Client('yutu75', '395243043y', '913618') #用户中心>>软件ID 生成一个替换 96001
im = open(img_path, 'rb').read() #本地图片文件路径 来替换 a.jpg 有时WIN系统须要//
return chaojiying.PostPic(im, img_type)['pic_str']
# 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 后要加()
def main():
chaojiying = Chaojiying_Client('自己的账号', '自己的密码', '913618') #用户中心>>软件ID 生成一个替换 96001
im = open('./img/wj.png', 'rb').read() #本地图片文件路径 来替换 a.jpg 有时WIN系统须要//
antiVerifyCheck = chaojiying.PostPic(im, 9103)['pic_str'].replace('|',",")
# print(antiVerifyCheck)
antiVerifyCheck=antiVerifyCheck.split(',')
antiVerifyCheck[1] = str(int(antiVerifyCheck[1])-30)
antiVerifyCheck[3] = str(int(antiVerifyCheck[3])-30)
antiVerifyCheck[5] = str(int(antiVerifyCheck[5])-30)
antiVerifyCheck = antiVerifyCheck[0]+","+antiVerifyCheck[1]+";"+antiVerifyCheck[2]+","+antiVerifyCheck[3]+";"+antiVerifyCheck[4]+","+antiVerifyCheck[5]
# print(antiVerifyCheck)
return antiVerifyCheck #1902 验证码类型 官方网站>>价格体系 3.4+版 print 后要加()
图片合成处理:
img.py:
# -*- coding: utf-8 -*-
# import cv2
# # 载入突破
# img = cv2.imread('test.jpg')
# # 给图片添加文字水印
# # cv2.putText(图像,需要添加字符串,需要绘制的坐标,字体类型,字号,字体颜色,字体粗细)
# img2 = cv2.putText(img, 'test', (100, 100), cv2.LINE_AA, 2, (249, 249, 249), 4)
# # 保存图片
# cv2.imwrite('wj.jpg', img2)
# # 下面是将生成的图片再弹窗中显示
# cv2.imshow('img',img2)
# cv2.waitKey(0)
# cv2.destoryAllWindows('img')
# __author__ = 'ShawDa'
from PIL import Image
from PIL import ImageFont
from PIL import Image
from PIL import ImageDraw
def join(png1, png2, flag='horizontal'):
"""
:param png1: path
:param png2: path
:param flag: horizontal or vertical
:return:
"""
img1, img2 = Image.open(png1), Image.open(png2)
size1, size2 = img1.size, img2.size
if flag == 'horizontal':
joint = Image.new('RGB', (size1[0]+size2[0], size1[1]))
loc1, loc2 = (0, 0), (size1[0], 0)
joint.paste(img1, loc1)
joint.paste(img2, loc2)
joint.save('./img/horizontal.png')
elif flag == 'vertical':
joint = Image.new('RGB', (size1[0], size1[1]+size2[1]))
loc1, loc2 = (0, 0), (0, size1[1])
joint.paste(img1, loc1)
joint.paste(img2, loc2)
joint.save('./img/vertical.png')
def img_main(code_name):
png1 = './img/code_white.png'
png2 = './img/code.png'
join(png1, png2, flag='vertical')
# 打开底版图片
imageFile = r'./img/vertical.png'
img = Image.open(imageFile)
# 选择字体与大小
font = ImageFont.truetype("C:\Windows\Fonts\msyh.ttc", 14)
# 在图片上添加文字
word = "请在下图依次点击:{}".format(code_name[0])
width = img.width
height = img.height
# 查看图片宽高
print(width, height)
position = (3, 10)
color = (0, 0, 0)
draw = ImageDraw.Draw(img)
draw.text(position, word, color, font=font)
# 保存图片
img.save(r'./img/wj.png')