熬夜爆肝两万字,建议收藏scrapy学习路线及其爬虫框架详解

Posted 孤寒rm-rf*

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了熬夜爆肝两万字,建议收藏scrapy学习路线及其爬虫框架详解相关的知识,希望对你有一定的参考价值。

ˏ₍•ɞ•₎ˎ有很多粉丝私聊我反馈说:"scrapy框架好难学,自己爬虫基础库已经学差不多了,实战也做了不少,但是好多外包或者老板都要求熟练使用scrapy框架,自己不知道如何下手!"

ˏ₍•ɞ•₎ˎ应粉丝们要求,我苦苦整理➕总结三天三夜,总结出本篇两万字文章,并在文末附带一整套scrapy框架学习路线,如果你能认认真真看完这篇文章,在心里对scrapy有个印象,然后潜心研究文末整套学习路线,那么,scrapy框架对你来说——手到擒来!!!




scrapy设计目的:用于爬取网络数据,提取结构性数据的框架,其中,scrapy使用了Twisted异步网络框架,大大加快了下载速度!

官方文档地址!!!

在这里插入图片描述

直接pip即可安装scrapy
命令:pip install scrapy

scrapy项目开发流程

创建项目:scrapy startproject mySpider
生成一个爬虫:scrapy genspider baidu baidu.com
提取数据:根据网站结构在spider中实现数据采集相关内容
保存数据:使用pipeline进行数据后续处理和保存

在这里插入图片描述
说明:(scrapy中每个模块的具体作用)

在这里插入图片描述

三个内置对象:(scrapy框架中只有三种数据类型)
request请求对象:由url,method,post_data,headers等构成
response响应对象:由url,body,status,headers等构成
item数据对象:本质是一个字典

流程原理描述:

    1.爬虫中起始的url构造的url对象-->爬虫中间件-->引擎-->调度器
    2.调度器把request-->引擎-->下载中间件-->下载器
    3.下载器发送请求,获取response响应--->下载中间件--->引擎-->爬虫中间件--->爬虫
    4.爬虫提取url地址,组装成request对象--->爬虫中间件--->引擎--->调度器,重复步骤2
    5.爬虫提取数据--->引擎--->管道处理和保存数据
爬虫中间件和下载中间件只是运行的逻辑的位置不同,作用是重复的:如替换UA等

1.创建项目

创建scrapy项目的命令:scrapy startproject <项目名字>
示例:scrapy startproject myspider

生成的目录和文件结果如下:

在这里插入图片描述

2.爬虫文件的创建

在项目路径下执行:
    scrapy genspider <爬虫名字> <允许爬取的域名>
    cd myspider
    scrapy genspider itcast itcast.cn

爬虫名字:作为爬虫运行时的参数
允许爬的域名:为对于爬虫设置的爬取范围,设置之后用于过滤要爬取的url,如果爬取的url与允许的域名不同,则被过滤掉

3.运行scrapy爬虫

命令:在项目目录下执行 scrapy crawl <爬虫名字>
示例:scrapy crawl itcast

编写itcast.py爬虫文件:

# -*- coding: utf-8 -*-
import scrapy


class ItcastSpider(scrapy.Spider):
    # 爬虫运行时的参数
    name = 'itcast'
    # 检查允许爬的域名
    allowed_domains = ['itcast.cn']
    # 1.修改设置起始的url
    start_urls = ['http://www.itcast.cn/channel/teacher.shtml#ajacaee']

    # 数据提取的方法:接收下载中间件传过来的response,定义对于网站相关的操作
    def parse(self, response):
        # 获取所有的教师节点
        t_list = response.xpath('//div[@class="li_txt"]')
        print(t_list)
        # 遍历教师节点列表
        tea_dist = {}
        for teacher in t_list:
            # xpath方法返回的是选择器对象列表     extract()方法可以提取到selector对象中data对应的数据。
            tea_dist['name'] = teacher.xpath('./h3/text()').extract_first()
            tea_dist['title'] = teacher.xpath('./h4/text()').extract_first()
            tea_dist['desc'] = teacher.xpath('./p/text()').extract_first()
            yield teacher




会发现已经可以OK运行!
在这里插入图片描述

4.数据建模后(明确了爬虫所爬取数据之后,使用管道进行数据持久化操作)

# -*- coding: utf-8 -*-
import scrapy
from ..items import UbuntuItem


class ItcastSpider(scrapy.Spider):
    # 爬虫运行时的参数
    name = 'itcast'
    # 检查允许爬的域名
    allowed_domains = ['itcast.cn']
    # 1.修改设置起始的url
    start_urls = ['http://www.itcast.cn/channel/teacher.shtml#ajacaee']

    # 数据提取的方法:接收下载中间件传过来的response,定义对于网站相关的操作
    def parse(self, response):
        # 获取所有的教师节点
        t_list = response.xpath('//div[@class="li_txt"]')
        print(t_list)
        # 遍历教师节点列表
        item = UbuntuItem()
        for teacher in t_list:
            # xpath方法返回的是选择器对象列表     extract()方法可以提取到selector对象中data对应的数据。
            item['name'] = teacher.xpath('./h3/text()').extract_first()
            item['title'] = teacher.xpath('./h4/text()').extract_first()
            item['desc'] = teacher.xpath('./p/text()').extract_first()
            yield item


注意:
    1.scrapy.Spider爬虫类中必须有名为parse的解析
    2.如果网站结构层次比较复杂,也可以自定义其他解析函数
    3.在解析函数中提取的url地址如果要发送请求,则必须属于allowed_domains范围内,但是start_urls中的url地址不受这个限制
    4.启动爬虫的时候注意启动的位置,是在项目路径下启动
    5.parse()函数中使用yield返回数据,注意:解析函数中的yield能够传递的对象只能是:BaseItem, Request, dict, None
    
定位元素以及提取数据、属性值的方法:
(解析并获取scrapy爬虫中的数据: 利用xpath规则字符串进行定位和提取)
    1.response.xpath方法的返回结果是一个类似list的类型,其中包含的是selector对象,操作和列表一样,但是有一些额外的方法
    2.额外方法extract():返回一个包含有字符串的列表
    3.额外方法extract_first():返回列表中的第一个字符串,列表为空没有返回None
    
response响应对象的常用属性:
    response.url:当前响应的url地址
    response.request.url:当前响应对应的请求的url地址
    response.headers:响应头
    response.requests.headers:当前响应的请求头
    response.body:响应体,也就是html代码,byte类型
    response.status:响应状态码

5.管道保存数据

在pipelines.py文件中定义对数据的操作
    1.定义一个管道类
    2.重写管道类的process_item方法
    3.process-item方法处理完item之后必须返回给引擎

在这里插入图片描述

升级:

# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html
import json

class UbuntuPipeline(object):

    def __init__(self):
        self.file = open('itcast.json', 'w', encoding='utf-8')


    def process_item(self, item, spider):
        # 将item对象强制转为字典,该操作只能在scrapy中使用
        item = dict(item)
        # 爬虫文件中提取数据的方法每yield一次,就会运行一次
        # 该方法为固定名称函数
        # 默认使用完管道,需要将数据返回给引擎
        # 1.将字典数据序列化
        '''ensure_ascii=False 将unicode类型转化为str类型,默认为True'''
        json_data = json.dumps(item, ensure_ascii=False, indent=2) + ',\\n'
        
        # 2.将数据写入文件
        self.file.write(json_data)
        
        return item
    
    def __del__(self):
        self.file.close()

6.settings.py配置启用管道

在settings文件中,解封代码,说明如下:

在这里插入图片描述

7.scrapy数据建模与请求

(通常在做项目的过程中,在items.py中进行数据建模)

 为什么建模?

    1.定义item即提前规划好哪些字段需要抓,防止手误,因为定义好之后,在运行过程中,系统会自动检查,值不相同会报错
    2.配合注释一起可以清晰的知道要抓取哪些字段,没有定义的字段不能抓取,在目标字段少的时候可以使用字典代替
    3.使用scrapy的一些特定组件需要Item做支持,如scrapy的ImagesPipeline管道类

在items.py文件中操作:

# -*- coding: utf-8 -*-

# Define here the models for your scraped items
#
# See documentation in:
# https://docs.scrapy.org/en/latest/topics/items.html

import scrapy


class UbuntuItem(scrapy.Item):
    # 讲师名字
    name = scrapy.Field()
    # 讲师职称
    title = scrapy.Field()
    # 讲师座右铭
    desc = scrapy.Field()
注意:
    1.from ..items import UbuntuItem这一行代码中 注意item的正确导入路径,忽略pycharm标记的错误
    2.python中的导入路径要诀:从哪里开始运行,就从哪里开始导入

8.设置user-agent

# settings.py文件中找到如下代码解封,并加入UA:
# Override the default request headers:
DEFAULT_REQUEST_HEADERS = {
  'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
  'Accept-Language': 'en',
  'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.162 Safari/537.36'

}

9.到目前为止,一个入门级别的scrapy爬虫已经OK了,基操都使用了!

现在cd到项目目录下,输入scrapy crawl itcast即可运行scrapy!

在这里插入图片描述

开发流程总结:

1.创建项目:
    scrapy startproject 项目名<br/>

2.明确目标:
    在items.py文件中进行建模

3.创建爬虫:

    创建爬虫
        scrapy genspider 爬虫名 允许的域
    完成爬虫
        修改start_urls    检查修改allowed_domains    编写解析方法

4.保存数据:
    在pipelines.py文件中定义对数据处理的管道
    在settings.py文件中注册启用管道

拓展:翻页请求的处理思路!

requests模块是如何实现翻页请求的:
    找到下一页的URL地址
    调用requests.get(url)


scrapy实现翻页的思路:
    找到下一页的url地址
    构造url地址的请求对象,传递给引擎
    
流程:
    1.确定url地址
    2.构造请求,scrapy.Request(url,callback)
        callback(回调):指定解析函数名称,表示该请求返回的响应使用哪一个函数进行解析

    3.把请求交给引擎(返回):yield scrapy.Request(url,callback)





实战:网易招聘爬虫

要求:获取招聘信息职位(职位,技术,地点等)

1.爬虫文件:

# -*- coding: utf-8 -*-
import scrapy
from ..items import WangyiItem

class WangyiSpider(scrapy.Spider):
    name = 'Wangyi'
    # 修改检查allowed_domains
    allowed_domains = ['163.com']
    start_urls = ['http://hr.163.com/position/list.do']

    def parse(self, response):
        # 提取数据
        # 获取所有职位节点列表
        node_list = response.xpath('//*[@class="position-tb"]/tbody/tr')
        # 遍历节点列表
        for num, node in enumerate(node_list):
            # 设置过滤条件,提取数据
            if num % 2 == 0:
                item = WangyiItem()
                item['name'] = node.xpath('./td[1]/a/text()').extract_first()
                # item['link'] = 'https://hr.163.com' + node.xpath('./td[1]/a/@href').extract_first()
                # response.urljoin 用于拼接相对路径的url,可以理解自动补全
                item['link'] = response.urljoin(node.xpath('./td[1]/a/@href').extract_first())
                item['depart'] = node.xpath('./td[2]/text()').extract_first()
                item['category'] = node.xpath('./td[3]/text()').extract_first()
                item['type_job'] = node.xpath('./td[4]/text()').extract_first()
                item['address'] = node.xpath('./td[5]/text()').extract_first()
                item['num'] = node.xpath('./td[6]/text()').extract_first().strip()
                item['date'] = node.xpath('./td[7]/text()').extract_first()
                yield item

        # 模拟翻页
        # last() 代表只取到最后一页    通过测试可知每次提取的part_url列表中最后一个值即为下一页的url
        part_url = response.xpath('/html/body/div[2]/div[2]/div[2]/div/a[last()]/@href').extract_first()
        # 判断是否最后一页
        if part_url != 'javascript:void(0)':
            next_url = response.urljoin(part_url)
            # 构建请求对象,并且返回给引擎
            yield scrapy.Request(
                url=next_url,
                # callback 不写默认使用parse方法
                callback=self.parse
            )

2.管道文件(保存数据):

# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html
import json

class WangyiPipeline(object):

    def __init__(self):
        self.file = open('wangyi.json', 'w', encoding='utf-8')

    def process_item(self, item, spider):
        item = dict(item)
        str_data = json.dumps(item, ensure_ascii=False, indent=2) + ',\\n'
        self.file.write(str_data)

        return item

3.items文件(说明要爬取的参数):

# -*- coding: utf-8 -*-

# Define here the models for your scraped items
#
# See documentation in:
# https://docs.scrapy.org/en/latest/topics/items.html

import scrapy


class WangyiItem(scrapy.Item):
    # define the fields for your item here like:
    # name = scrapy.Field()
    # 职位名称
    name = scrapy.Field()
    # 详情页
    link = scrapy.Field()
    # 部门
    depart = scrapy.Field()
    # 类别
    category = scrapy.Field()
    # 工作类型
    type_job = scrapy.Field()
    # 工作地点
    address =scrapy.Field()
    # 人数
    num = scrapy.Field()
    # 发布时间
    date = scrapy.Field()

3.settings.py文件中的配置:

一关协议:
在这里插入图片描述二设UA:
在这里插入图片描述
三开管道:
在这里插入图片描述

4.scrapy crawl Wangyi运行发现OK:

在这里插入图片描述






1.scrapy.Request的更多参数

源码:

scrapy.Request(url,[callback,method=“GET”,headers,body,cookies,meta,dont_filter=False])

参数解释
    1.中括号里的参数为可选参数
    2.callback:表示当前的url的响应交给哪个函数去处理
    3.meta:实现数据在不同的解析函数中传递,meta默认带有部分数据,比如下载延迟,请求深度等是个字典参数
    4.dont_filter:默认为False,会过滤请求的url地址,即请求过的url地址不会继续被请求,对需要重复请求的url地址可以把它设置为Ture,比如贴吧的翻页请求,页面的数据总是在变化;start_urls中的地址会被反复请求,否则程序不会启动
    5.method:指定POST或GET请求
    6.headers:接收一个字典,其中不包括cookies
    7.cookies:接收一个字典,专门放置cookies
    8.body:接收json字符串,为POST的数据,发送payload_post请求时使用

meta参数的使用:

作用:meta可以实现数据在不同的解析函数中的传递

    1.meta参数是一个字典
    2.meta字典中有一个固定的键proxy,表示代理ip,关于代理ip的使用我们将在看到scrapy的下载中间件时进行介绍

项目实战中使用:网易招聘爬虫升级版!

1.更改爬虫文件如下:
# -*- coding: utf-8 -*-
import scrapy
from ..items import WangyiItem

class WangyiSpider(scrapy.Spider):
    name = 'Wangyi'
    # 修改检查allowed_domains
    allowed_domains = ['163.com']
    start_urls = ['http://hr.163.com/position/list.do']

    def parse(self, response):
        # 提取数据
        # 获取所有职位节点列表
        node_list = response.xpath('//*[@class="position-tb"]/tbody/tr')
        # 遍历节点列表
        for num, node in enumerate(node_list):
            # 设置过滤条件,提取数据
            if num % 2 == 0:
                item = WangyiItem()
                item['name'] = node.xpath('./td[1]/a/text()').extract_first()
                # item['link'] = 'https://hr.163.com' + node.xpath('./td[1]/a/@href').extract_first()
                # response.urljoin 用于拼接相对路径的url,可以理解自动补全
                item['link'] = response.urljoin(node.xpath('./td[1]/a/@href').extract_first())
                item['depart'] = node.xpath('./td[2]/text()').extract_first()
                item['category'] = node.xpath('./td[3]/text()').extract_first()
                item['type_job'] = node.xpath('./td[4]/text()').extract_first()
                item['address'] = node.xpath('./td[5]/text()').extract_first()
                item['num'] = node.xpath('./td[6]/text()').extract_first().strip()
                item['date'] = node.xpath('./td[7]/text()').extract_first()
                # 构建详情页的请求
                yield scrapy.Request(url=item['link'],
                                     callback=self.parse_detail,
                                     meta={
                                         'item':item,
                                         # 'proxy':'HTTP://123.149.137.96:9999',
                                     })

        # 模拟翻页
        # last() 代表只取到最后一页    通过测试可知每次提取的part_url列表中最后一个值即为下一页的url
        part_url = response.xpath('/html/body/div[2]/div[2]/div[2]/div/a[last()]/@href').extract_first()
        # 判断是否最后一页
        if part_url != 'javascript:void(0)':
            next_url = response.urljoin(part_url)
            # 构建请求对象,并且返回给引擎
            yield scrapy.Request(
                url=next_url,
                # callback 不写默认使用parse方法
                callback=self.parse
            )

    def parse_detail(self,response):
        print('--------',response.meta['item'])
        # 将meta传参获取
        item = response.meta['item']
        # 提取数据
        item['duty'] = response.xpath('/html/body/div[2]/div[2]/div[1]/div/div/div[2]/div[1]/div/text()').extract()
        item['require'] = response.xpath('/html/body/div[2]/div[2]/div[1]/div/div/div[2]/div[2]/div/text()').extract()
        print(item)
        yield item
2.更新items.py文件如下:
# -*- coding: utf-8 -*-

# Define here the models for your scraped items
#
# See documentation in:
# https://docs.scrapy.org/en/latest/topics/items.html

import scrapy


class WangyiItem(scrapy.Item):
    # define the fields for your item here like:
    # name = scrapy.Field()
    # 职位名称
    name = scrapy.Field()
    # 详情页
    link = scrapy.Field()
    # 部门
    depart = scrapy.Field()
    # 类别
    category = scrapy.Field()
    # 工作类型
    type_job = scrapy.Field()
    # 工作地点
    address =scrapy.Field()
    # 人数
    num = scrapy.Field()
    # 发布时间
    date = scrapy.Field()

    duty = scrapy.Field()
    require = scrapy.Field()

3.运行:(会发现现在多了duty,require数据!)

在这里插入图片描述

2.scrapy模拟登录(登录github为例)

requests模块是如何实现模拟登陆的?
    1.直接携带cookies请求页面
    2.找url地址,发送post请求存储cookie


scrapy的模拟登陆:
    1.直接携带cookies
    2.找url地址,发送post请求存储cookie


scrapy携带cookies直接获取需要登陆后的页面
应用场景:
    1.cookie过期时间很长,常见于一些不规范的网站
    2.能在cookie过期之前把所有的数据拿到
    3.配合其他程序使用,比如其使用selenium把登陆之后的cookie获取到保存到本地,scrapy发送请求之前先读取本地cookie

在这里插入图片描述
通过获取标题获取是否登录成功状态!

注意
    1.cookies从request headers中获取,在配置文件settings中,添加user-agent,注释掉robot协议
    2.对应的,如果start_url地址中的url是需要登录后才能访问的url地址,则需要重写start_request方法并在其中手动添加上cookie
    3.scrapy中cookie不能够放在headers中,在构造请求的时候有专门的cookies参数,能够接受字典形式的coookie

3.scrapy.Request发送post请求

通过scrapy.Request()指定method、body参数来发送post请求;但是通常使用scrapy.FormRequest()来发送post请求!

发送post请求:
    注意:scrapy.FormRequest()能够发送表单和ajax请求,参考阅读 https://www.jb51.net/article/146769.htm


思路分析:
    1.找到post的url地址:点击登录按钮进行抓包,然后定位url地址为https://github.com/session
    2.找到请求体的规律:分析post请求的请求体,其中包含的参数均在前一次的响应中
    3.是否登录成功:通过请求个人主页,观察是否包含用户名

登录github为例:

# -*- coding: utf-8 -*-
import scrapy
from ..items import UbuntuItem


class ItcastSpider(scrapy.Spider):
    # 爬虫运行时的参数
    name = 'itcast'
    # 检查允许爬的域名
    allowed_domains = ['github.com']
    # 1.修改设置起始的url
    start_urls = ['https://github.com/login']

    def parse(self, response):
        # 从登陆页面响应中解析post数据
        token = response.xpath('//*[@id="login"]/form/input[1]/@value').extract_first()
        post_data = {
            'commit': 'Sign in',
            'authenticity_token': token,
            'login': '1915344876@qq.com',
            'password': '在这输自己的密码',
            'webauthn - support': 'supported',
        }
        print(post_data)
        # 针对登陆url发送post请求
        yield scrapy❤️两万字《算法 + 数据结构》全套路线❤️(建议收藏)

课程设计一夜C语言爆肝两万字 《建议收藏!》

熬夜爆肝!C++核心进阶知识点汇总整理万字干货预警 建议收藏

爆肝两万字,我爷爷都看的懂的《栈和队列》,建议各位观众姥爷先收藏

爆肝两万字,我爷爷都看的懂的《栈和队列》,建议各位观众姥爷先收藏

HTML两万字王者笔记大总结❤️熬夜整理&建议收藏❤️(下篇)