小白爬虫scrapy第四篇

在上篇中没有说到启动如何去启动,scrapy是使用cmd命令行去启动的
我们用scrapy的cmdline去启动
命名point.pyhtml

# 导入cmdline 中的execute用来执行cmd命令
from scrapy.cmdline import execute
# 执行cmd命令参数为[ scrapy, 爬虫, 爬虫名称]
execute(['scrapy', 'crawl', 'AiquerSpider'])

这个文件放在项目根目录下
如图:
clipboard.png
若是各位同窗按照个人前面几篇的步骤写完的话能够用这个去测试一下(把部分代码注释去了),你会发现有好多神秘的蓝色连接,哇啊啊啊啊!!!!!个人右手在燃烧!!!!!!!python

先在我们去保存数据吧!我这几天写项目需求写到崩溃就不去作具体数据处理了,直接贴代码数据库

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

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

class AiquerPipeline(object):
    def __init__(self):
        # 打开文件
        self.file = open('data.json', 'w', encoding='utf-8')

    def process_item(self, item, spider):
        # 读取item中的数据
        line = json.dumps(dict(item), ensure_ascii=False) + "\n"
        # 写入文件
        self.file.write(line)
        # 返回item
        return item

        # 该方法在spider被开启时被调用。
        def open_spider(self, spider):

            pass

        # 该方法在spider被关闭时被调用。
        def close_spider(self, spider):

            pass

在运行这个东西以前是要注册的,回到settings.py里面找到Configure item pipelines,将下面的注释去掉就好了,我们没有具体需求因此不用改优先级别json

# Configure item pipelines
# See http://scrapy.readthedocs.org/en/latest/topics/item-pipeline.html
ITEM_PIPELINES = {
    'AiQuer.pipelines.AiquerPipeline': 300,
}

AiQuer.pipelines.AiquerPipeline是为你要注册的类,右侧的’300’为该Pipeline的优先级,范围1~1000,越小越先执行。
没有作具体数据处理了,直接把他们保存为json数据了,很长很长一段眼花
下一篇是如何去保存在数据库中scrapy

相关文章
相关标签/搜索