scrapy+selenium+超级鹰验证码识别登录古诗文网

文章目录

一、改写超级鹰api接口

代码如下,main中注释的部分为原部分

#!/usr/bin/env python
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')
        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 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__':
    chaojiying = Chaojiying_Client('用户名', '密码', '914982')    #用户中心>>软件ID 生成一个替换 96001
    im = open('a.jpg', 'rb').read()                                                 #本地图片文件路径 来替换 a.jpg 有时WIN系统须要//
    print (chaojiying.PostPic(im, 1902))                                                #1902 验证码类型  官方网站>>价格体系 3.4+版 print 后要加()

def get_captcha(image_name):
    chaojiying = Chaojiying_Client('用户名', '密码', '914982')    #用户中心>>软件ID 生成一个替换 96001
    im = open(image_name, 'rb').read()
    return chaojiying.PostPic(im, 8001)['pic_str']

这样引用chaojiying.py这个文件,调用get_captcha()方法,传入图片名称就可以返回验证码数据了。

二、用scrapy+selenium+超级鹰登录古诗文网

分为三个步骤:

  1. selenium登录网址,并且将整个网页截屏
  2. 获取验证码图片在网页中的x,y坐标位置,打开整页截屏的图片,通过该坐标截取验证码
  3. 引入超级鹰的api,将图片名称传给get_captcha()方法,返回验证码信息

代码如下:

import scrapy
import time
from selenium import webdriver
from PIL import Image
import chaojiying

class LoginSpider(scrapy.Spider):
    name = 'login'
    allowed_domains = ['gushiwen.cn']
    start_urls = ['https://so.gushiwen.cn/user/login.aspx']
    personal = 'https://so.gushiwen.cn/user/collect.aspx'

    def start_requests(self):
        driver = webdriver.Chrome()
        driver.get(self.start_urls[0])

        driver.save_screenshot('gushiwen.png')
        imgElement = driver.find_element_by_id('imgCode')
        left = imgElement.location['x']
        top = imgElement.location['y']
        right = left + imgElement.size['width']
        bottom = top + imgElement.size['height']
        box = (left, top, right, bottom)
        screencut = Image.open('gushiwen.png')
        screencut.crop(box).save("截图后.png")

        code = chaojiying.get_captcha('截图后.png')

        driver.find_element_by_id('email').send_keys('你的用户名')
        time.sleep(1)
        driver.find_element_by_id('pwd').send_keys('你的密码')
        time.sleep(1)
        driver.find_element_by_id('code').send_keys(code)
        time.sleep(1)
        driver.find_element_by_id('denglu').click()
        time.sleep(1)

        cookies_dict = {cookie['name']: cookie['value'] for cookie in driver.get_cookies()}
        driver.close()

        yield scrapy.Request(url=self.personal,cookies=cookies_dict,callback=self.parse)

    def parse(self, response):
        print('这是在收藏中:',response.url)

用selenium登录获取的cookeis继续访问我的收藏,验证是否登录成功

scrapy+selenium+超级鹰验证码识别登录古诗文网
可以看到,这里已经跳转到我的收藏中了,代表登录成功

三、总结

以上便是所有内容,有帮助的小伙伴可以点个赞,谢谢

Original: https://blog.csdn.net/wyl201010417/article/details/116903070
Author: 板栗呀
Title: scrapy+selenium+超级鹰验证码识别登录古诗文网

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/790495/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球