05-打码平台的使用、scrapy介绍安装

发布时间 2024-01-02 11:28:23作者: Way*yy

打码平台

数字字母类的验证码可以使用python模块:ddddocr
计算题,成语题,滑块。。。:第三方打码平台,人工操作

打码平台
	-云打码,超级鹰

超级鹰SDK

import requests
from hashlib import md5


class ChaojiyingClient(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__':
    chaojiying = ChaojiyingClient('18436093205', 'fuhua0501.', '903641')  # 用户中心>>软件ID 生成一个替换 96001
    im = open('a.jpg', 'rb').read()  # 本地图片文件路径 来替换 a.jpg 有时WIN系统须要//
    print(chaojiying.PostPic(im, 1004))  # 1902 验证码类型  官方网站>>价格体系 3.4+版 print 后要加()
    # print chaojiying.PostPic(base64_str, 1902)  #此处为传入 base64代码

打码平台自动登录打码平台

import time

from selenium import webdriver
from selenium.webdriver.common.by import By
from PIL import Image
from cjy_sdk import ChaojiyingClient

bro = webdriver.Chrome()
bro.get("https://www.chaojiying.com/user/login/")
bro.maximize_window()
bro.implicitly_wait(10)

username = bro.find_element(By.NAME, "user")
password = bro.find_element(By.NAME, "pass")
code = bro.find_element(By.NAME, "imgtxt")

username.send_keys("18436093205")
password.send_keys("fuhua0501.")

# 获取到页面中的图片验证码
# 1、截取整个页面
bro.save_screenshot("main.png")
# 2、找到图片的位置
img = bro.find_element(By.XPATH,
                       "/html/body/div[3]/div/div[3]/div[1]/form/div/img")

location = img.location
size = img.size
print("图片的位置是:", location, "图片的大小是:", size)

# 获取到图片验证码的准确位置
img_code = (
    int(location['x']), int(location['y']), int(location['x'] + size['width']), int(location['y'] + size['height'])
)
# 使用pillow模块打开这张图片
img = Image.open("./main.png")
# 抠出来图片验证码
farm = img.crop(img_code)
# 保存验证码
farm.save("code.png")

# 调用超级鹰打码平台
chaojiying = ChaojiyingClient('18436093205', 'fuhua0501.', '903641')
im = open('code.png', 'rb').read()
code_ = chaojiying.PostPic(im, 1004)

try:
    if code_.get("err_no") == 0:
        code.send_keys(code_.get("pic_str"))
        submit = bro.find_element(By.CSS_SELECTOR,
                                  "body > div.wrapper_danye > div > div.content_login > div.login_form > form > "
                                  "p:nth-child(5) > input")
        submit.click()
        bro.refresh()
        time.sleep(5)
    bro.close()
except Exception as e:
    print(e)

selenium爬取京东商品信息


scrapy介绍安装

# 爬虫模块:requests   bs4  selenium 

# 爬虫框架--->不是模块--->类似于django--->爬虫界的django(跟djagno非常像)

# 安装:pip3 install scrapy
    -mac,linux 非常好装
    -win:看人品:因为twisted装不上
    若安装失败按一下步骤操作:
        1、pip3 install wheel #安装后,便支持通过wheel文件安装软件,wheel文件官网:https://www.lfd.uci.edu/~gohlke/pythonlibs
        3、pip3 install lxml
        4、pip3 install pyopenssl
        5、下载并安装pywin32:https://sourceforge.net/projects/pywin32/files/pywin32/
        6、下载twisted的wheel文件:http://www.lfd.uci.edu/~gohlke/pythonlibs/#twisted
        7、执行pip3 install 下载目录\Twisted-17.9.0-cp36-cp36m-win_amd64.whl
        8、pip3 install scrapy
        
# 装完后---->scripts文件夹下就会有scrapy可执行文件--->类似于(django-admin.exe)
	-创建项目,创建爬虫都用它
    
# 介绍
Scrapy一个开源和协作的框架,其最初是为了页面抓取 (更确切来说, 网络抓取 )所设计的,使用它可以以快速、简单、可扩展的方式从网站中提取所需的数据。但目前Scrapy的用途十分广泛,可用于如数据挖掘、监测和自动化测试等领域,也可以应用在获取API所返回的数据或者通用的网络爬虫。

Scrapy 是基于twisted框架开发而来,twisted是一个流行的事件驱动的python网络框架。因此Scrapy使用了一种非阻塞(又名异步)的代码来实现并发。整体架构大致如下


## 架构

# 1 爬虫(SPIDERS)
SPIDERS是开发人员自定义的类,用来解析responses,并且提取items,或者发送新的请求
开发人员,主要在这写代码:设置爬取的地址,解析爬取完成的数据,继续爬取或者存储数据


#2  引擎(EGINE)
引擎负责控制系统所有组件之间的数据流,并在某些动作发生时触发事件

# 3 调度器(SCHEDULER)
用来接受引擎发过来的请求, 压入队列中, 并在引擎再次请求的时候返回. 可以想像成一个URL的优先级队列, 由它来决定下一个要抓取的网址是什么, 同时去除重复的网址

# 4 下载器(DOWLOADER)
用于下载网页内容, 并将网页内容返回给EGINE,下载器是建立在twisted这个高效的异步模型上的


# 5 项目管道(ITEM PIPLINES)
在items被提取后负责处理它们,主要包括清理、验证、持久化(比如存到数据库)等操作

# 6 下载器中间件(Downloader Middlewares)

位于Scrapy引擎和下载器之间,主要用来处理从EGINE传到DOWLOADER的请求request,已经从DOWNLOADER传到EGINE的响应response,你可用该中间件做以下几件事

# 7 爬虫中间件(Spider Middlewares)
位于EGINE和SPIDERS之间,主要工作是处理SPIDERS的输入(即responses)和输出(即requests)

scrapy目录结构

# https://www.cnblogs.com/robots.txt 爬虫协议


# 创建scrapy项目
	scrapy startproject scrapy_demo
# 创建爬虫--->可以创建多个爬虫
	scrapy genspider 爬虫名 爬虫地址  # django创建app
    scrapy genspider cnblogs www.cnblogs.com  # 创建了爬取cnblogs的爬虫
# 运行爬虫:默认直接爬取www.cnblogs.com--->后续需要写代码实现
	scrapy crawl 爬虫名字
    scrapy crawl cnblogs 
# 目录结构
	scrapy_demo  #项目名
        scrapy_demo #包
            __init__.py
            spiders #包 ,所有爬虫文件,放在这里
                __init__.py
                cnblogs.py
            items.py # 一个个类,等同于djagno的models
            pipelines.py # 写如何存储,存到拿
            settings.py# 配置文件
            middlewares.py # 中间件:两种
     	scrapy.cfg	 # 上线用的