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,如果没有请查看安装指南.. ...
随机推荐
- C#避免WinForm窗体假死
WinForm窗体在使用过程中如果因为程序等待时间太久而导致窗体本身假死无法控制,会严重影响用户的体验,这种情况大多是UI线程被耗时长的代码操作占用所致,可以新开一个线程用来完成耗时长的操作,然后再将 ...
- 解决JAVA连接Sybase数据库查询数据乱码的问题
连接字符串加上charset=eucgb&jconnect_version=0例如:jdbc:sybase:Tds:server:port/database?charset=eucgb& ...
- Fortify漏洞之XML External Entity Injection(XML实体注入)
继续对Fortify的漏洞进行总结,本篇主要针对 XML External Entity Injection(XML实体注入) 的漏洞进行总结,如下: 1.1.产生原因: XML External ...
- 小数据玩转Pyspark(2)
一.客户画像 客户画像应用:精准营销(精准预测.个性化推荐.联合营销):风险管控(高风险用户识别.异常用户识别.高可疑交易识别):运营优化(快速决策.产品组合优化.舆情分析.服务升级):业务创新(批量 ...
- obj = obj || {} 分析这个代码的起到的作用
情况一: <script> function test(obj) { console.log(obj.value) } function student() { this.value = ...
- 【SpringMVC】参数绑定
一.概述 1.3 参数绑定过程 1.2 @RequestParam 二.自定义绑定使用属性编辑器 2.1 使用WebDataBinder(了解) 2.2 使用WebBindingInitializer ...
- Hive数据库操作
Hive数据结构 除了基本数据类型(与java类似),hive支持三种集合类型 Hive集合类型数据 array.map.structs hive (default)> create table ...
- Linux 磁盘、分区、文件系统、挂载
磁盘 Linux所有设备都被抽象成为一个文件,保存在/dev目录下. 设备名称一般为hd[a-z]或sd[a-z].如果电脑中有多硬盘,则设备名依次为sda.adb.sdc...以此类推 IDE设备的 ...
- 异常-Caused by: org.apache.hadoop.ipc.RemoteException(org.apache.hadoop.security.AccessControlException): Permission denied: user=hdfs, access=WRITE, inode="/hbase":root:supergroup:drwxr-xr-x
1 详细异常 Caused by: org.apache.hadoop.ipc.RemoteException(org.apache.hadoop.security.AccessControlExce ...
- P1080 【NOIP 2012】 国王游戏[贪心+高精度]
题目来源:洛谷 题目描述 恰逢 H国国庆,国王邀请n 位大臣来玩一个有奖游戏.首先,他让每个大臣在左.右手上面分别写下一个整数,国王自己也在左.右手上各写一个整数.然后,让这 n 位大臣排成一排,国王 ...