python爬虫,验证码识别,携带cookies请求

古诗词网案例!!!

识别验证码类型:

python 复制代码
# 此处用到的图片验证码识别网址为:http://ttshitu.com/ 图鉴
import base64
import json
import requests
# 一、图片文字类型(默认 3 数英混合):
# 1 : 纯数字
# 1001:纯数字2
# 2 : 纯英文
# 1002:纯英文2
# 3 : 数英混合
# 1003:数英混合2
#  4 : 闪动GIF
# 7 : 无感学习(独家)
# 11 : 计算题
# 1005:  快速计算题
# 16 : 汉字
# 32 : 通用文字识别(证件、单据)
# 66:  问答题
# 49 :recaptcha图片识别
# 二、图片旋转角度类型:
# 29 :  旋转类型
#
# 三、图片坐标点选类型:
# 19 :  1个坐标
# 20 :  3个坐标
# 21 :  3 ~ 5个坐标
# 22 :  5 ~ 8个坐标
# 27 :  1 ~ 4个坐标
# 48 : 轨迹类型
#
# 四、缺口识别
# 18 : 缺口识别(需要2张图 一张目标图一张缺口图)
# 33 : 单缺口识别(返回X轴坐标 只需要1张图)
# 五、拼图识别
# 53:拼图识别
def base64_api(uname, pwd, img, typeid):
    with open(img, 'rb') as f:
        base64_data = base64.b64encode(f.read())
        b64 = base64_data.decode()
    data = {"username": uname, "password": pwd, "typeid": typeid, "image": b64}
    result = json.loads(requests.post("http://api.ttshitu.com/predict", json=data).text)
    if result['success']:
        return result["data"]["result"]
    else:
        return result["message"]
    return ""


if __name__ == "__main__":
    # 获取验证码图片
    code_url = 'https://so.gushiwen.cn/RandCode.ashx'
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36',
    }
    sess = requests.Session()  # 携带cookies
    res = sess.get(code_url, headers=headers)
    # 保存验证码图片到当前路径
    with open('yzm.jpg', 'wb') as f:
        f.write(res.content)
    img_path = "./yzm.jpg"
    result = base64_api(uname='图鉴网账号', pwd='图鉴网密码', img=img_path, typeid=3)
    print(result)
    # 这里的result为识别后的验证码数字
    # 上半部分是咱们刚才的代码
    # 需要爬取网站的登录页面
    url = 'https://so.gushiwen.cn/user/login.aspx?from=http%3a%2f%2fso.gushiwen.cn%2fuser%2fcollect.aspx'

    data = {
        '__VIEWSTATE': 'gRid5bo91a/3jGuyGRrb5K/ONANSyHJgAOW160cLBWy/6dnfzslD1VMDZFCAwg6zRoZwi9lGa/pn1woHDhFvBctNo/ugDw7KM39GNrvmebKfE2cyB2BMd7e98B4=',
        '__VIEWSTATEGENERATOR': 'C93BE1AE',
        'from': 'http://so.gushiwen.cn/user/collect.aspx',
        'email': '古诗词账号',
        'pwd': '古诗词密码',
        'code': result,
        'denglu': '登录',
    }
    res = sess.post(url, data=data, headers=headers)
    with open('gsw.html', 'wb') as f:
        f.write(res.content)
相关推荐
hboot4 小时前
AI工程师第二课 - 数据处理
人工智能·python·数据分析
用户8356290780518 小时前
使用 Python 自动化 PowerPoint 形状布局与格式设置
后端·python
用户83562907805110 小时前
用 Python 自动化 PowerPoint 演讲者备注添加
后端·python
太岁又沐风12 小时前
复现并修掉ART hook框架 Pine 调用原方法时的偶发 SIGSEGV
爬虫
黄忠16 小时前
01-系统架构设计-LangGraph状态机与多源异构RAG
python
zzzzzz31016 小时前
假如我是掘金管理员,我先给评论区装个'代码审查'系统
python·程序员·机器人
砍材农夫16 小时前
python环境|conda安装和使用(2)
后端·python
程序员龙叔1 天前
编写高质量 Skill 系列 -- 如何设计需求分析与用例生成的 SKILL
自动化测试·软件测试·python·软件测试工程师·接口测试·性能测试·skill·ai测试
用户8356290780511 天前
使用 Python 操作 Word 内容控件
后端·python
LDR0061 天前
Type-C 快充全面升级!LDR6601 赋能个人护理便携电机,重塑剃须刀 / 理发器新体验
c语言·开发语言