Scrapy基础知识
1、简介
官网简介:
Scrapy 是一个用于抓取网站和提取结构化数据的应用程序框架,可用于各种有用的应用程序,如数据挖掘、信息处理或历史存档
简而言之就是一个稳定、高效的爬虫框架,提高爬取效率,大型的爬虫项目都需要用到它。拓展性好,使用Twisted异步网络框架,极大的提升了下载速度。
2、五大组件
五大核心组件:
爬虫(Spider): 解析响应数据, 提取URL, 提取数据
引擎(Engine): 调度各个模块, 实现数据(请求,响应,数据)在各个模块间传递
调度器(Scheduler): 缓存请求, 请求去重
下载器(Downloader): 发送请求, 获取响应数据
管道(ItemPipline): 处理数据(爬虫提取的数据) , 比如保存数据
两个中间件:
下载器中间件(Downloader Middlewares): 在引擎 和 下载器之间, 作用:反反爬(对请求和响应处理)
爬虫中间件(Spider Middlewares): 在引擎 和 爬虫之间, 对请求和响应进行过滤的!
Scrapy内部基本流程:
1爬虫文件把请求(url)交给引擎,
2引擎把请求再交给调度器,调度器进行排序类似队列,
3调度器把请求再交给引擎,
4引擎把请求给下载器进行下载,
5下载器下载完把响应交给引擎,
6引擎把响应交给爬虫文件进行数据分析,
7爬虫文件把处理好的响应再交给引擎,
8引擎把响应给管道文件进行存储。
scrapy框架创建
1、创建scrapy项目
scrapy startproject 项目名称
scrapy genspider 文件名称 【域名】
使用pycharm打开终端操作
创建了一个名为“firstBlood”的scarpy项目
和一个名为“first”的爬虫文件
创建成功后;
2、简单入门
在first文件中已自动生成框架
import scrapy
class FirstSpider(scrapy.Spider):
# 爬虫文件名称
# 源文件的唯一标识,不可重复
name = 'first'
# 允许爬取的start_urls中的域名
allowed_domains = ['www.xxx.com']
# 想要爬取的url列表
# 列表中url会被scrapy自动发送请求
start_urls = ['https://www.baidu.com/', 'https://www.sogou.com/']
# 进行数据解析,response参数表示的是请求成功后对应的响应对象
def parse(self, response):
print(response)
运行代码:
scrapy crawl first
结果
2021-06-09 15:35:51 [scrapy.utils.log] INFO: Scrapy 2.5.0 started (bot: firstBlood)
2021-06-09 15:35:51 [scrapy.utils.log] INFO: Versions: lxml 4.6.3.0, libxml2 2.9.5, cssselect 1.1.0, parsel 1.6.0, w3l
ib 1.22.0, Twisted 21.2.0, Python 3.8.5 (tags/v3.8.5:580fbb0, Jul 20 2020, 15:57:54) [MSC v.1924 64 bit (AMD64)], pyOp
enSSL 20.0.1 (OpenSSL 1.1.1k 25 Mar 2021), cryptography 3.4.7, Platform Windows-10-10.0.19041-SP0
2021-06-09 15:35:51 [scrapy.utils.log] DEBUG: Using reactor: twisted.internet.selectreactor.SelectReactor
2021-06-09 15:35:51 [scrapy.crawler] INFO: Overridden settings:
{'BOT_NAME': 'firstBlood',
'NEWSPIDER_MODULE': 'firstBlood.spiders',
'ROBOTSTXT_OBEY': True,
'SPIDER_MODULES': ['firstBlood.spiders']}
2021-06-09 15:35:51 [scrapy.extensions.telnet] INFO: Telnet Password: 7c686a6d257d0ace
2021-06-09 15:35:52 [scrapy.middleware] INFO: Enabled extensions:
['scrapy.extensions.corestats.CoreStats',
'scrapy.extensions.telnet.TelnetConsole',
'scrapy.extensions.logstats.LogStats']
2021-06-09 15:35:52 [scrapy.middleware] INFO: Enabled downloader middlewares:
['scrapy.downloadermiddlewares.robotstxt.RobotsTxtMiddleware',
'scrapy.downloadermiddlewares.httpauth.HttpAuthMiddleware',
'scrapy.downloadermiddlewares.downloadtimeout.DownloadTimeoutMiddleware',
'scrapy.downloadermiddlewares.defaultheaders.DefaultHeadersMiddleware',
'scrapy.downloadermiddlewares.useragent.UserAgentMiddleware',
'scrapy.downloadermiddlewares.retry.RetryMiddleware',
'scrapy.downloadermiddlewares.redirect.MetaRefreshMiddleware',
'scrapy.downloadermiddlewares.httpcompression.HttpCompressionMiddleware',
'scrapy.downloadermiddlewares.redirect.RedirectMiddleware',
'scrapy.downloadermiddlewares.cookies.CookiesMiddleware',
'scrapy.downloadermiddlewares.httpproxy.HttpProxyMiddleware',
'scrapy.downloadermiddlewares.stats.DownloaderStats']
2021-06-09 15:35:52 [scrapy.middleware] INFO: Enabled spider middlewares:
['scrapy.spidermiddlewares.httperror.HttpErrorMiddleware',
'scrapy.spidermiddlewares.offsite.OffsiteMiddleware',
'scrapy.spidermiddlewares.referer.RefererMiddleware',
'scrapy.spidermiddlewares.urllength.UrlLengthMiddleware',
'scrapy.spidermiddlewares.depth.DepthMiddleware']
2021-06-09 15:35:52 [scrapy.middleware] INFO: Enabled item pipelines:
[]
2021-06-09 15:35:52 [scrapy.core.engine] INFO: Spider opened
2021-06-09 15:35:52 [scrapy.extensions.logstats] INFO: Crawled 0 pages (at 0 pages/min), scraped 0 items (at 0 items/m
in)
2021-06-09 15:35:52 [scrapy.extensions.telnet] INFO: Telnet console listening on 127.0.0.1:6023
2021-06-09 15:35:52 [scrapy.core.engine] DEBUG: Crawled (200) <GET https://www.baidu.com/robots.txt> (referer: None)
2021-06-09 15:35:52 [scrapy.downloadermiddlewares.robotstxt] DEBUG: Forbidden by robots.txt: <GET https://www.baidu.co
m/>
2021-06-09 15:35:53 [scrapy.core.engine] DEBUG: Crawled (200) <GET https://www.sogou.com/robots.txt> (referer: None)
2021-06-09 15:35:53 [scrapy.downloadermiddlewares.robotstxt] DEBUG: Forbidden by robots.txt: <GET https://www.sogou.co
m/>
2021-06-09 15:35:53 [scrapy.core.engine] INFO: Closing spider (finished)
2021-06-09 15:35:53 [scrapy.statscollectors] INFO: Dumping Scrapy stats:
{'downloader/exception_count': 2,
'downloader/exception_type_count/scrapy.exceptions.IgnoreRequest': 2,
'downloader/request_bytes': 446,
'downloader/request_count': 2,
'downloader/request_method_count/GET': 2,
'downloader/response_bytes': 1909,
'downloader/response_count': 2,
'downloader/response_status_count/200': 2,
'elapsed_time_seconds': 1.56094,
'finish_reason': 'finished',
'finish_time': datetime.datetime(2021, 6, 9, 7, 35, 53, 982346),
'httpcompression/response_bytes': 13288,
'httpcompression/response_count': 2,
'log_count/DEBUG': 4,
'log_count/INFO': 10,
'response_received_count': 2,
'robotstxt/forbidden': 2,
'robotstxt/request_count': 2,
'robotstxt/response_count': 2,
'robotstxt/response_status_count/200': 2,
'scheduler/dequeued': 2,
'scheduler/dequeued/memory': 2,
'scheduler/enqueued': 2,
'scheduler/enqueued/memory': 2,
'start_time': datetime.datetime(2021, 6, 9, 7, 35, 52, 421406)}
2021-06-09 15:35:53 [scrapy.core.engine] INFO: Spider closed (finished)
没成功,因为权限被设置了,遵循了robot协议,要去配置中关掉
ROBOTSTXT_OBEY = False
不遵循协议
LOG_LEVEL = 'ERROR'
只显示错误提醒,和运行结果