1、学了尚硅谷视频后,自己来动动手,相关视频链接如下
088_尚硅谷_爬虫_requests_cookie登陆古诗文网_哔哩哔哩_bilibili
2、先去超级鹰注册账号,然后购买提分,一元=1000积分,可以少买点玩玩
超级鹰验证码识别-专业的验证码云端识别服务,让验证码识别更快速、更准确、更强大
这个页面下载相关语言的代码,这里是python
3、代码如下,为了省事,直接在下载的超级鹰代码chaojiying.py里写的
#!/usr/bin/env python
# coding:utf-8
import requests
from hashlib import md5
import urllib.request
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 PostPic_base64(self, base64_str, codetype):
"""
im: 图片字节
codetype: 题目类型 参考 http://www.chaojiying.com/price.html
"""
params = {
'codetype': codetype,
'file_base64':base64_str
}
params.update(self.base_params)
r = requests.post('http://upload.chaojiying.net/Upload/Processing.php', data=params, 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__':
#设置head
headersLogin={
'user-agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36'
};
#登录页面,先调用登录页面,获取session,在调用验证码图片接口
loginUrl="https://www.chaojiying.com/user/login/";
#验证码接口
codeUrl="https://www.chaojiying.com/include/code/code.php?u=1";
session=requests.session();
responseLogin=session.post(url="https://www.chaojiying.com/user/login/",headers=headersLogin)
responseCode=session.get(codeUrl);
responseCodeImg=responseCode.content;
#获取验证码写入本地图片
with open('code.png','wb') as fp:
fp.write(responseCodeImg)
#调用超级鹰接口识别二维码
chaojiying = Chaojiying_Client('超级鹰用户名', '超级鹰用户名的密码', '96001') #用户中心>>软件ID 生成一个替换 96001
im = open('code.png', 'rb').read()
imag=chaojiying.PostPic(im, 1902)#本地图片文件路径 来替换 a.jpg 有时WIN系统须要//
print(imag)
paramsLogin={
'user':'超级鹰用户名',
'pass':'超级鹰用户名的密码',
'imgtxt':imag['pic_str'],#获取识别的图片验证码内容
'act':'1'
};
#调用登录接口
response=session.post(url="https://www.chaojiying.com/user/login/",headers=headersLogin,data=paramsLogin)
print(response.text)
4、运行完后打印登录成功页面
5、完事了~~