编写qsbk_spider.py爬虫文件

# -*- coding: utf-8 -*-
import scrapy
from qsbk.items import QsbkItem
from scrapy.http.response.html import HtmlResponse
from scrapy.selector.unified import SelectorList

class QsbkSpiderSpider(scrapy.Spider):
    name = 'qsbk_spider'
    allowed_domains = ['qiushibaike.com']
    start_urls = ['https://www.qiushibaike.com/text/']

    def parse(self, response):
        duanzidiv=response.xpath("//div[@id='content-left']/div")
        for duanzidivs in duanzidiv:
            author=duanzidivs.xpath(".//h2/text()").get().strip()
            content=duanzidivs.xpath(".//div[@class='content']//text()").getall()
            content="".join(content).strip()
           #调用pipelines文件,存取数据到json文件里面
            # duanzi={"author":author,"content":content}
            item=QsbkItem(author=author,content=content)
            yield item

编写items.py文件

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

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

import scrapy


class QsbkItem(scrapy.Item):
    # define the fields for your item here like:
    # name = scrapy.Field()
    author=scrapy.Field()
    content=scrapy.Field()

编写pipelines.py文件保存数据到duanzi.json文件里

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

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

import json
class QsbkPipeline(object):
    def __init__(self):
        self.fp=open("duanzi.json",'w',encoding='utf-8')

    def open_spider(self,spider):
        print("爬虫开始了...")

    def process_item(self, item, spider):
        item_json=json.dumps(dict(item),ensure_ascii=False)
        self.fp.write(item_json+'n')
        return item

    def close_spider(self,spider):
        self.fp.close()
        print("爬虫结束了...")

编写start.py爬虫启动文件

# -*- coding:utf-8 -*-
#作者:    baikai  
#创建时间: 2018/12/14 9:16 
#文件:    start.py  
#IDE:    PyCharm
from scrapy import cmdline

# cmdline.execute("scrapy crawl shuju_spider".split())
cmdline.execute(["scrapy","crawl","qsbk_spider"])

设置settings.py文件相关配置

运行start.py文件爬取网站数据并保存到duanzi.json文件里

 

 

# Scrapy笔记
## 安装scrapy框架:
1. 安装`scrapy`:通过`pip install scrapy`即可安装。
2. 如果在windows下,还需要安装`pypiwin32`,如果不安装,那么以后运行scrapy项目的时候就会报错。安装方式:`pip install pypiwin32`。
3. 如果是在ubuntu下,还需要安装一些第三方库:`sudo apt-get install python-dev python-pip libxml2-dev libxslt1-dev zlib1g-dev libffi-dev libssl-dev`。

## 创建项目和爬虫:
1. 创建项目:`scrapy startproject [爬虫的名字]`。
2. 创建爬虫:进入到项目所在的路径,执行命令:`scrapy genspider [爬虫名字] [爬虫的域名]`。注意,爬虫名字不能和项目名称一致。

## 项目目录结构:
1. items.py:用来存放爬虫爬取下来数据的模型。 
2. middlewares.py:用来存放各种中间件的文件。 
3. pipelines.py:用来将items的模型存储到本地磁盘中。 
4. settings.py:本爬虫的一些配置信息(比如请求头、多久发送一次请求、ip代理池等)。 
5. scrapy.cfg:项目的配置文件。 
6. spiders包:以后所有的爬虫,都是存放到这个里面。

## 糗事百科Scrapy爬虫笔记:
1. response是一个`scrapy.http.response.html.HtmlResponse`对象。可以执行`xpath`和`css`语法来提取数据。
2. 提取出来的数据,是一个`Selector`或者是一个`SelectorList`对象。如果想要获取其中的字符串。那么应该执行`getall`或者`get`方法。
3. getall方法:获取`Selector`中的所有文本。返回的是一个列表。
4. get方法:获取的是`Selector`中的第一个文本。返回的是一个str类型。
5. 如果数据解析回来,要传给pipline处理。那么可以使用`yield`来返回。或者是收集所有的item。最后统一使用return返回。
6. item:建议在`items.py`中定义好模型。以后就不要使用字典。
7. pipeline:这个是专门用来保存数据的。其中有三个方法是会经常用的。
    * `open_spider(self,spider)`:当爬虫被打开的时候执行。
    * `process_item(self,item,spider)`:当爬虫有item传过来的时候会被调用。
    * `close_spider(self,spider)`:当爬虫关闭的时候会被调用。
    要激活piplilne,应该在`settings.py`中,设置`ITEM_PIPELINES`。示例如下:
    ```python
    ITEM_PIPELINES = {
       'qsbk.pipelines.QsbkPipeline': 300,
    }
    ```

## JsonItemExporter和JsonLinesItemExporter:
保存json数据的时候,可以使用这两个类,让操作变得得更简单。
1. `JsonItemExporter`:这个是每次把数据添加到内存中。最后统一写入到磁盘中。好处是,存储的数据是一个满足json规则的数据。坏处是如果数据量比较大,那么比较耗内存。
示例代码如下: ```python
from scrapy.exporters import JsonItemExporter class QsbkPipeline(object): def __init__(self): self.fp = open("duanzi.json",'wb') self.exporter = JsonItemExporter(self.fp,ensure_ascii=False,encoding='utf-8') self.exporter.start_exporting() def open_spider(self,spider): print('爬虫开始了...') def process_item(self, item, spider): self.exporter.export_item(item) return item def close_spider(self,spider): self.exporter.finish_exporting() self.fp.close() print('爬虫结束了...') ``` 2. `JsonLinesItemExporter`:这个是每次调用`export_item`的时候就把这个item存储到硬盘中。坏处是每一个字典是一行,整个文件不是一个满足json格式的文件。
好处是每次处理数据的时候就直接存储到了硬盘中,这样不会耗内存,数据也比较安全。示例代码如下: ```python
from scrapy.exporters import JsonLinesItemExporter class QsbkPipeline(object): def __init__(self): self.fp = open("duanzi.json",'wb') self.exporter = JsonLinesItemExporter(self.fp,ensure_ascii=False,encoding='utf-8') def open_spider(self,spider): print('爬虫开始了...') def process_item(self, item, spider): self.exporter.export_item(item) return item def close_spider(self,spider): self.fp.close() print('爬虫结束了...') ```

 

内容来源于网络如有侵权请私信删除
你还没有登录,请先登录注册
  • 还没有人评论,欢迎说说您的想法!