Scrapy 教程(11)-API启动爬虫
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启动爬虫的更多相关文章
- Python爬虫框架Scrapy教程(1)—入门
最近实验室的项目中有一个需求是这样的,需要爬取若干个(数目不小)网站发布的文章元数据(标题.时间.正文等).问题是这些网站都很老旧和小众,当然也不可能遵守 Microdata 这类标准.这时候所有网页 ...
- Scrapy框架实战-妹子图爬虫
Scrapy这个成熟的爬虫框架,用起来之后发现并没有想象中的那么难.即便是在一些小型的项目上,用scrapy甚至比用requests.urllib.urllib2更方便,简单,效率也更高.废话不多说, ...
- python爬虫随笔(2)—启动爬虫与xpath
启动爬虫 在上一节中,我们已经创建好了我们的scrapy项目,看着这一大堆文件,想必很多人都会一脸懵逼,我们应该怎么启动这个爬虫呢? 既然我们采用cmd命令创建了scrapy爬虫,那就得有始有终有逼格 ...
- node-webkit教程(11)Platform Service之shell
node-webkit教程(11)Platform Service之shell 文/玄魂 目录 node-webkit教程(10)Platform Service之shell 前言 11.1 She ...
- scrapy 命令行创建 启动 跟踪
不是python文件中的,而是在虚拟机中运行的命令行,先要workon进入虚拟环境 2.scrapy 框架的使用 -1.新建项目 命令:scrapy startproject <project_ ...
- Miniconda安装scrapy教程
一.背景说明 前两天想重新研究下Scrapy,当时的环境是PyCharm社区版+Python 3.7.使用pip安装一直报错 “distutils.errors.DistutilsPlatformEr ...
- scrapy 安装流程和启动
#Windows平台 1. pip3 install wheel #安装后,便支持通过wheel文件安装软件,wheel文件官网:https://www.lfd.uci.edu/~gohlke/pyt ...
- 基于Scrapy框架的Python新闻爬虫
概述 该项目是基于Scrapy框架的Python新闻爬虫,能够爬取网易,搜狐,凤凰和澎湃网站上的新闻,将标题,内容,评论,时间等内容整理并保存到本地 详细 代码下载:http://www.demoda ...
- Scrapy教程
Scrapy教程 原文地址https://doc.scrapy.org/en/latest/intro/tutorial.html 此教程我们假设你已经装好了Scrapy,如果没有请查看安装指南.. ...
随机推荐
- 在论坛中出现的比较难的sql问题:46(日期条件出现的奇怪问题)
原文:在论坛中出现的比较难的sql问题:46(日期条件出现的奇怪问题) 最近,在论坛中,遇到了不少比较难的sql问题,虽然自己都能解决,但发现过几天后,就记不起来了,也忘记解决的方法了. 所以,觉得有 ...
- 笔记 - C#从头开始构建编译器 - 2
视频与PR:https://github.com/terrajobst/minsk/blob/master/docs/episode-02.md 作者是 Immo Landwerth(https:// ...
- linux环境weblogic的安装及新建域
环境:inux 64位,jdk 64位, jdk 安装用户应使用weblogic.若使用其他用户安装,须将jdk安装目录整体授权给wblogic用户 安装包:wls1036_dev.zi ...
- Java Web-Ajax学习
Java Web-Ajax学习 概念 Ajax(Asynchronous JavaScript And XML,异步的JavaScript和XML). 异步和同步:在客户端和服务器端相互通信的基础上来 ...
- 使用docker安装gitlab
我这里使用的系统是centos7 首先安装docker,docker-compose(非必须,但是使用它可以简化镜像启动参数),需要注意的是docker-compose安装依赖Python的pip,所 ...
- 后端参数校验器v1.0(调用一个方法校验所有参数并得到校验结果,且包括错误原因)
一:介绍 在写后端时,面对多个参数,比如手机号码.密码等我们常常需要写验证逻辑,当需要验证的参数较多的时候我们会需要写很多的判断语句,这就造成了大量的代码冗余.因此我开发了一套参数验证器,只需要调用参 ...
- 类中变量私有化和调用:__x和getx/setx或者property
__xx:双前置下划线,子类不可继承属性.方法,父类私有. 详见:https://www.cnblogs.com/andy9468/p/8299448.html 例子1:隐藏数据:私有化后,用get和 ...
- Vue学习官网和Vue的书籍 目录结构
Vue基础知识学习网站[中文] https://cn.vuejs.org/v2/guide/ Vue路由知识学习网站[中文] https://router.vuejs.org/zh/guide/ V ...
- MNIST手写数据集在运行中出现问题解决方案
今天在运行手写数据集的过程中,出现一个问题,代码没有问题,但是运行的时候一直报错,错误如下: urllib.error.URLError: <urlopen error [SSL: CERTIF ...
- Python_if语句
1.if语句: color='红色' if color=='蓝色': print('我是蓝色') elif color=='红色': print('我是红色') else : print('未知') ...