scarpy 不仅提供了 scrapy crawl spider 命令来启动爬虫,还提供了一种利用 API 编写脚本 来启动爬虫的方法。

scrapy 基于 twisted 异步网络库构建的,因此需要在 twisted 容器内运行它。

可以通过两个 API 运行爬虫:scrapy.crawler.CrawlerProcess  和  scrapy.crawler.CrawlerRunner

scrapy.crawler.CrawlerProcess

这个类内部将会开启 twisted.reactor、配置log 和 设置 twisted.reactor 自动关闭,该类是所有 scrapy 命令使用的类。

运行单个爬虫示例

class QiushispiderSpider(scrapy.Spider):
name = 'qiushiSpider'
# allowed_domains = ['qiushibaike.com']
start_urls = ['https://tianqi.2345.com/'] def start_requests(self):
return [scrapy.Request(url=self.start_urls[0], callback=self.parse)] # def parse(self, response):
print('proxy simida') if __name__ == '__main__':
from scrapy.crawler import CrawlerProcess
process = CrawlerProcess()
process.crawl(QiushispiderSpider) # 'qiushiSpider'
process.start()

process.crawl() 内的参数可以是 爬虫名'qiushiSpider',也可以是 爬虫类名QiushispiderSpider

这种方式并没有使用爬虫的配置文件settings

2019-05-27 14:39:57 [scrapy.crawler] INFO: Overridden settings: {}

获取配置

from scrapy.crawler import CrawlerProcess
from scrapy.utils.project import get_project_settings
process = CrawlerProcess(get_project_settings())
process.crawl(QiushispiderSpider) # 'qiushiSpider'
process.start()

运行多个爬虫

import scrapy
from scrapy.crawler import CrawlerProcess class MySpider1(scrapy.Spider):
    ... class MySpider2(scrapy.Spider):
    ... process = CrawlerProcess()
process.crawl(MySpider1)
process.crawl(MySpider2)
process.start()

scrapy.crawler.CrawlerRunner

1. 更好的控制爬虫运行过程

2. 显式运行 twisted.reactor,显式关闭 twisted.reactor

3. 需要在 CrawlerRunner.crawl 返回的对象中添加回调函数

运行单个爬虫示例

class QiushispiderSpider(scrapy.Spider):
name = 'qiushiSpider'
# allowed_domains = ['qiushibaike.com']
start_urls = ['https://tianqi.2345.com/'] def start_requests(self):
return [scrapy.Request(url=self.start_urls[0], callback=self.parse)] # def parse(self, response):
print('proxy simida') if __name__ == '__main__':
# test CrawlerRunner
from twisted.internet import reactor
from scrapy.crawler import CrawlerRunner
from scrapy.utils.log import configure_logging
from scrapy.utils.project import get_project_settings configure_logging({'LOG_FORMAT':'%(levelname)s: %(message)s'})
runner = CrawlerRunner(get_project_settings()) d = runner.crawl(QiushispiderSpider)
d.addBoth(lambda _: reactor.stop())
reactor.run() # the script will block here until the crawling is finished

configure_logging 设定日志输出格式

addBoth 添加 关闭 twisted.crawl 的回调函数

运行多个爬虫

import scrapy
from twisted.internet import reactor
from scrapy.crawler import CrawlerRunner
from scrapy.utils.log import configure_logging class MySpider1(scrapy.Spider):
    ... class MySpider2(scrapy.Spider):
    ... configure_logging()
runner = CrawlerRunner()
runner.crawl(MySpider1)
runner.crawl(MySpider2)
d = runner.join()
d.addBoth(lambda _: reactor.stop())
reactor.run() # the script will block here until all crawling jobs are finished

也可以异步实现

from twisted.internet import reactor, defer
from scrapy.crawler import CrawlerRunner
from scrapy.utils.log import configure_logging class MySpider1(scrapy.Spider):
    ... class MySpider2(scrapy.Spider):
    ... configure_logging()
runner = CrawlerRunner() @defer.inlineCallbacks
def crawl():
    yield runner.crawl(MySpider1)
    yield runner.crawl(MySpider2)
    reactor.stop() crawl()
reactor.run() # the script 

参考资料:

https://blog.csdn.net/weixin_33857230/article/details/89571872

Scrapy 教程(11)-API启动爬虫的更多相关文章

  1. Python爬虫框架Scrapy教程(1)—入门

    最近实验室的项目中有一个需求是这样的,需要爬取若干个(数目不小)网站发布的文章元数据(标题.时间.正文等).问题是这些网站都很老旧和小众,当然也不可能遵守 Microdata 这类标准.这时候所有网页 ...

  2. Scrapy框架实战-妹子图爬虫

    Scrapy这个成熟的爬虫框架,用起来之后发现并没有想象中的那么难.即便是在一些小型的项目上,用scrapy甚至比用requests.urllib.urllib2更方便,简单,效率也更高.废话不多说, ...

  3. python爬虫随笔(2)—启动爬虫与xpath

    启动爬虫 在上一节中,我们已经创建好了我们的scrapy项目,看着这一大堆文件,想必很多人都会一脸懵逼,我们应该怎么启动这个爬虫呢? 既然我们采用cmd命令创建了scrapy爬虫,那就得有始有终有逼格 ...

  4. node-webkit教程(11)Platform Service之shell

    node-webkit教程(11)Platform Service之shell 文/玄魂 目录 node-webkit教程(10)Platform Service之shell 前言 11.1  She ...

  5. scrapy 命令行创建 启动 跟踪

    不是python文件中的,而是在虚拟机中运行的命令行,先要workon进入虚拟环境 2.scrapy 框架的使用 -1.新建项目 命令:scrapy startproject <project_ ...

  6. Miniconda安装scrapy教程

    一.背景说明 前两天想重新研究下Scrapy,当时的环境是PyCharm社区版+Python 3.7.使用pip安装一直报错 “distutils.errors.DistutilsPlatformEr ...

  7. scrapy 安装流程和启动

    #Windows平台 1. pip3 install wheel #安装后,便支持通过wheel文件安装软件,wheel文件官网:https://www.lfd.uci.edu/~gohlke/pyt ...

  8. 基于Scrapy框架的Python新闻爬虫

    概述 该项目是基于Scrapy框架的Python新闻爬虫,能够爬取网易,搜狐,凤凰和澎湃网站上的新闻,将标题,内容,评论,时间等内容整理并保存到本地 详细 代码下载:http://www.demoda ...

  9. Scrapy教程

    Scrapy教程 原文地址https://doc.scrapy.org/en/latest/intro/tutorial.html 此教程我们假设你已经装好了Scrapy,如果没有请查看安装指南.. ...

随机推荐

  1. ORA-00001:unique constraint violated解决

    转自:https://www.2cto.com/database/201211/172340.html ORA-00001:unique constraint violated解决 今天往数据库中插入 ...

  2. Linux基础命令汇总109条

    1       文件管理 1.1     basename 1.1.1     功能说明 从文件名中去掉路径和扩展名 例:basename include/stdio.h .h Output &quo ...

  3. Android笔记(八) Android中的布局——相对布局

    RelativeLayout又称为相对布局,也是一种常用的布局形式.和LinearLayout的排列规则不同,RelativeLayout显得更加随意一下,它通常通过相对定位 的方式让控件出现在布局的 ...

  4. php 图片与base64互转

    header('Content-type:text/html;charset=utf-8'); //读取图片文件,转换成base64编码格式 $image_file = '1.png'; $image ...

  5. 剖析可执行文件ELF组成

    对比参考:剖析.o文件ELF组成 相比.o的ELF格式,有哪些变化? .rel.text和.rel.data消失了 为什么这两个节会消失? 链接器将各.o中同名的.text和.data节整合到一起时, ...

  6. DotnetCore下Grpc的简单使用(基于3.0版本)

    目录: 一.简单介绍DotnetCore3.0如何将.proto文件生成对应的服务端和客户端类 二.介绍如何在服务端使用Grpc,以及Grpc需要的条件(HTTP2.TLS) 三.介绍如何创建Grpc ...

  7. windows平台RSA密钥生成

    1.安装openssl工具 下载安装openssl工具,执行安装目录bin下的"openssl.exe",执行后会弹出命令窗口. 2.生成私钥 在命令窗口输入“genrsa -ou ...

  8. 织梦阿里云OSS解决方案

    准备工作 申请OSS账号,并且创建一个public-read的bucket.这里需要权限为public-read是因为后面需要匿名访问. 详细步骤 1.开启织梦远程附件功能2.现在织梦还有远程附件还有 ...

  9. win10 水晶报表安装包

    windows 10 64 VS2013安装 CR For VS 13_0_18 安装过程没有报错 安装成功http://downloads.businessobjects.com/akdlm/cr4 ...

  10. 获取历史K线数据的几个方法

    1.通过已有的股票交易软件下载数据,如果他们是开源结构的,就可以解析他们的K线数据. 2.在互联网上抓取数据 int iStockCode;CString strUrl; 通过OpenUrl.Read ...