爬虫学习-验证码识别

  • 反爬机制:验证码,识别验证码图片中的数据,用于模拟登陆

    • 识别验证码的操作

  • 人工肉眼识别(不推荐)

  • 第三方自动识别(推荐)

  • python第三方库:tesseract、ddddocr

  • 使用方法

  • 超级鹰使用教程

  • 1、注册登录

  • 2、购买题分

  • 3、用户中心-》软件ID-》生成ID-》提交

  • 4、开发文档-》python的Demo下载

  • 5、放到同级目录里,调一下

  • 使用打码平台识别验证码的编码流程:

  • 将验证码图片进行本地下载

  • 调用平台提供的示例代码,进行图片数据识别

  • 例子

  • 获取古诗文网验证码

# 识别古诗文网验证码登录
import requests
from lxml import etree
import ddddocr
from chaojiying import Chaojiying_Client

if __name__ == '__main__':
    # 获取验证码图片,并保存验证码图片到本地

    url = 'https://so.gushiwen.cn/user/login.aspx?from=http://so.gushiwen.cn/user/collect.aspx'
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36 Edg/108.0.1462.54'
    }
    page_text = requests.get(url=url, headers=headers).text
    tree = etree.HTML(page_text)
    img_src = tree.xpath('//*[@id="imgCode"]/@src')[0]
    img_src = 'https://so.gushiwen.cn' + img_src
    print(img_src)
    img_data = requests.get(url=img_src, headers=headers).content
    with open('a.jpg', 'wb') as fp:
        fp.write(img_data)
    # # 调用打码平台的示例层序进行验证码图片数据识别
    # chaojiying = Chaojiying_Client('账户', '密码', '943457')  # 用户中心>>软件ID 生成一个替换 96001
    # im = open('a.jpg', 'rb').read()  # 本地图片文件路径 来替换 a.jpg 有时WIN系统须要//
    # code_tag = chaojiying.PostPic(im, 1902)  # 1902 验证码类型  官方网站>>价格体系 3.4+版 print 后要加()
    # # print(code_tag)
    # # print(code_tag['pic_str'])
    # code = code_tag['pic_str']
    ocr = ddddocr.DdddOcr()
    with open('a.jpg', 'rb') as f:
        img_bytes = f.read()
    res = ocr.classification(img_bytes)
    print(res)
  • 对古诗文网进行模拟登录

import ddddocr
import requests
from lxml import etree

if __name__ == '__main__':
    # 1、获取验证码图片的文字数据
    url = 'https://so.gushiwen.cn/user/login.aspx?from=http://so.gushiwen.cn/user/collect.aspx'
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36 Edg/108.0.1462.54'
    }
    session = requests.Session()
    page_text = session.get(url=url, headers=headers).text
    tree = etree.HTML(page_text)
    img_src = 'https://so.gushiwen.cn' + tree.xpath('//*[@id="imgCode"]/@src')[0]
    # print(img_src)
    code_data = session.get(url=img_src, headers=headers).content
    with open('./a.jpg', 'wb') as fp:
        fp.write(code_data)
    ocr = ddddocr.DdddOcr()
    with open('a.jpg', 'rb') as f:
        img_bytes = f.read()
    code = ocr.classification(img_bytes)
    print(code)
    # 2、对post请求进行发送(处理请求参数)
    post_url = 'https://so.gushiwen.cn/user/login.aspx?from=http://so.gushiwen.cn/user/collect.aspx'
    param = {
        '__VIEWSTATE': 'LEVhj4L7rIz3Bvtl1Qic94SRQ5LHyhp7oQWBifUJ + 3zDwdL8028kq2H2W6DgoZ9dus1rxfSVJQ8uQ1lzvRRLhN7GzwDpp9NUOXorj + Wa92FiThQWYzr0LDyce + 66vAvYO1 / rlwt4q0ul6 + jImgjl7J6ndHs =',
        '__VIEWSTATEGENERATOR': 'C93BE1AE',
        'from': 'http://so.gushiwen.cn/user/collect.aspx',
        'email': '账户',
        'pwd': '密码',
        'code': code,
        'denglu': '登录',
    }
    login_text = session.post(url=post_url, params=param, headers=headers)
    print(login_text.status_code)
    login_text = login_text.text
    url2 = 'https://so.gushiwen.cn/user/collect.aspx'
    login_text2 = session.post(url=url2, headers=headers).text
    print(login_text2)
    with open('./古诗文.html', 'w', encoding='utf-8') as fp:
        fp.write(login_text2)
    # 3、对响应数据进行持久化储存

  • 点击登录按钮之后或发起一个post请求

  • post请求中会携带登录之前录入的相关的登录信息(用户名、密码、验证码.....)

  • 验证码:每次请求都会变化

    • cookie相关操作

  • http/https协议特性:无状态

  • 没有请求到对应页面数据的原因:发起的第二次基于个人主页页面请求的时候,服务器并不知道该请求是基于登录状态下的请求

  • cookie:用来让服务器端记录下客户端的相关状态

  • 添加cookie

  • 一、手动添加:通过抓包工具获取cookie值,将该值封装到headers中,requests中作为参数

  • 二、自动添加

  • cookie值来源:模拟登录post请求后,由服务器端创建

  • session会话对象:

  • 可以进行请求发送

  • 如果请求过程中产生了cookie,则该cookie会被自动储存/携带在该session对象中

  • 使用方法:

  • 0、创建一个session对象:session=requests.Session()

  • 1、使用session对象进行模拟登录post请求的发送(cookie就会被储存在session对象中)

  • 2、使用已经储存了cookie的session对象在对相应页面进行get请求发送(携带了cookie)

    • 代理:破解封ip这种反爬机制

  • 什么是代理:代理服务器

  • 代理的作用:

  • 突破自身ip访问的限制

  • 可以隐藏自身真实的ip

  • 代理相关网站

  • 快代理

  • 西祠代理

  • 代理类型:

  • http:应用到http协议对应的url当中

  • https:应用到https协议对应的url当中

  • 代理ip的匿名度

  • 透明:服务器知道该次请求使用了代理,也知道该次请求对应的真实ip

  • 匿名:服务器知道该次请求使用了代理,不知道该次请求对应的真实ip

  • 高匿名:服务器不知道该次请求使用了代理,更不知道该次请求对应的真实ip

猜你喜欢

转载自blog.csdn.net/qq_61897309/article/details/128552232