scrapy框架的基本使用

什么是框架?
    - 就是一个具有很强通用性且集成了很多功能的项目模板(可以被应用在各种需求中)

- scrapy集成好的功能:
    - 高性能的数据解析操作(xpath)
    - 高性能的数据下载
    - 高性能的持久化存储
    - 中间件
    - 全栈数据爬取操作
    - 分布式:redis
    - 请求传参的机制(深度爬取)
    - scrapy中合理的应用selenium
- 环境的安装
          a. pip3 install wheel

          b. 下载twisted http://www.lfd.uci.edu/~gohlke/pythonlibs/#twisted

          c. 进入下载目录,执行 pip3 install Twisted‑17.1.0‑cp35‑cp35m‑win_amd64.whl

          d. pip3 install pywin32

           e. pip3 install scrapy

 

- 创建工程
    - scrapy startproject ProName(项目名)
    - cd ProName
    - scrapy genspider spiderName www.xxx.com #:创建爬虫文件
    - 执行:scrapy crawl spiderName
    - settings:
        - 不遵从robots协议
        - UA伪装
        - LOG_LEVEL = 'ERROR'

 - scrapy的数据解析
    - extract():列表是有多个列表元素
    - extract_first():列表元素只有单个
 - scrapy的持久化存储
    - 基于终端指令:
        - 只可以将parse方法的返回值存储到磁盘文件中
        - scrapy crawl first -o file.csv
    - 基于管道:pipelines.py
        - 编码流程:
            - 1.数据解析

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

class FirstSpider(scrapy.Spider):
    #爬虫文件的名称:爬虫源文件的唯一标识
    name = 'first'
    #允许的域名:
    # allowed_domains = ['www.baidu.com']
    #起始的url列表:列表中的列表元素会被scrapy自动的进行请求发送
    start_urls = ['https://dig.chouti.com/']
    #解析数据
    #基于终端指令的持久化存储
    # def parse(self, response):
    #     data_list = []
    #     div_list = response.xpath('/html/body/main/div/div/div[1]/div/div[2]/div[1]')
    #     for div in div_list:
    #         #注意:xpath返回的列表中的列表元素是Selector对象,我们要解析获取的字符串的数据是存储在该对象中
    #         #必须经过一个extract()的操作才可以将改对象中存储的字符串的数据获取
    #         # content = div.xpath('./div/div/div[1]/a/text()')[0].extract()
    #         content = div.xpath('./div/div/div[1]/a/text()').extract_first()
    #         #xpath返回的列表中的列表元素有多个(Selector对象),想要将每一个列表元素对应的Selector中的字符串取出改如何操作?response.xpath('/div//text()').extract()
    #         print(content) #<Selector xxx='dsdsd' data="短发hi的说法绝对是">
    #         data_list.append(content)
    #     return data_list

    #管道的持久化存储
    def parse(self, response):
        div_list = response.xpath('/html/body/main/div/div/div[1]/div/div[2]/div[1]')
        for div in div_list:
            #注意:xpath返回的列表中的列表元素是Selector对象,我们要解析获取的字符串的数据是存储在该对象中
            #必须经过一个extract()的操作才可以将改对象中存储的字符串的数据获取
            # content = div.xpath('./div/div/div[1]/a/text()')[0].extract()
            content = div.xpath('./div/div/div[1]/a/text()').extract_first()
            item = FirstbloodItem()
            item['content'] = content
            #xpath返回的列表中的列表元素有多个(Selector对象),想要将每一个列表元素对应的Selector中的字符串取出改如何操作?response.xpath('/div//text()').extract()
            print(content) #<Selector xxx='dsdsd' data="短发hi的说法绝对是">

            yield item #将item对象提交给管道





            - 2.在item的类中定义相关的属性

# -*- 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 FirstbloodItem(scrapy.Item):
    # define the fields for your item here like:
    content = scrapy.Field()#Field是一个万能的数据类型


            - 3.将解析的数据存储封装到item类型的对象中.item['p']

            - 4.将item对象提交给管道
            - 5.在管道类中的process_item方法负责接收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

#专门用作于持久化存储
class FirstbloodPipeline(object):
    fp = None
    def open_spider(self,spider):
        print('我只会在爬虫开始的时候执行一次!!!')
        self.fp = open('./data.txt','w',encoding='utf-8')
    def process_item(self, item, spider):
        content = item['content']
        self.fp.write(content)
        return item
    def close_spider(self,spider):
        print('我只会在爬虫结束的时候调用一次1!!')
        self.fp.close()





            - 6.在配置文件中开启管道
         - 细节补充:
            - 管道文件中的一个管道类表示将数据存储到某一种形式的平台中。
            - 如果管道文件中定义了多个管道类,爬虫类提交的item会给到优先级最高的管道类。
            - process_item方法的实现中的return item的操作表示将item传递给下一个即将被执行的管道类

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值