小说网站建设笺池斋,2022客翻番的推广方法,网站建设代码怎么导入图片,公司logo和商标一样吗认识scrapyscrapy是一个为了爬取网站数据#xff0c;提取结构性数据而编写的应用框架#xff0c;我们只需实现少量的代码#xff0c;就能实现数据的快速抓取scrapy使用了Twisted异步网络架构#xff0c;可以加快下载速度 pip install twisted安装#xff1a;pip install s…认识scrapyscrapy是一个为了爬取网站数据提取结构性数据而编写的应用框架我们只需实现少量的代码就能实现数据的快速抓取scrapy使用了Twisted异步网络架构可以加快下载速度 pip install twisted安装pip install scrapy流程图组件介绍组件作用Scrapy Engine(引擎)负责Spider、ItemPipeline、Downloader、Scheduler中间的通讯信号、数据传递等Scheduler(调度器)它负责接受引擎发送过来的Request请求并按照一定的方式进行整理排列入队当引擎需要时交还给引擎Downloader(下载器)负责下载(引擎)发送的所有Requests请求并将其获取到的Responses交还给Scrapy Engine(引擎)由引擎交给Spider来处理。Spider(爬虫)它负责处理所有Responses,从中分析提取数据获取Item字段需要的数据并将需要跟进的URL提交给引擎再次进入SchedulerItem Pipeline(管道)它负责处理Spider中获取到的Item并进行进行后期处理详细分析、过滤、存储等的地方Downloader Middlewares下载中间件一个可以自定义扩展下载功能的组件。Spider Middlewares(Spider中间件)一个可以自定扩展和操作引擎和Spider中间通信的功能组件运行流程引擎HiSpider, 你要处理哪一个网站Spider老大要我处理xxxx.com。引擎你把第一个需要处理的URL给我吧。Spider给你第一个URL是xxxxxxx.com。引擎Hi调度器我这有request请求你帮我排序入队一下。调度器好的正在处理你等一下。引擎Hi调度器把你处理好的request请求给我。调度器给你这是我处理好的request引擎Hi下载器你按照老大的下载中间件的设置帮我下载一下这个request请求下载器好的给你这是下载好的东西。如果失败sorry这个request下载失败了。然后引擎告诉调度器这个request下载失败了你记录一下我们待会儿再下载引擎HiSpider这是下载好的东西并且已经按照老大的下载中间件处理过了你自己处理一下注意这儿responses默认是交给**def parse()**这个函数处理的Spider处理完毕数据之后对于需要跟进的URLHi引擎我这里有两个结果这个是我需要跟进的URL还有这个是我获取到的Item数据。引擎Hi 管道 我这儿有个item你帮我处理一下调度器这是需要跟进URL你帮我处理下。然后从第四步开始循环直到获取完老大需要全部信息。管道调度器好的现在就做快速入门创建一个scrapy项目scrapy startproject name # name 为项目名称创建一个spidercd name # 进入创建的项目scrapy genspider spider_name url # spider_name为爬虫名url为要抓取的目标网站scrapy crawl spider_name # 执行爬虫spider手动创建的spider文件import scrapyclass BdSpiderSpider(scrapy.Spider):name spider_nameallowed_domains [baidu.com]start_urls [https://www.baidu.com]def parse(self, response,**kwargs):pass**name**标识spider。它在一个项目中必须是唯一的即不能为不同的爬行器设置相同的名称**allowed_domains**允许爬取url的域名**start_urls**一个url列表spider从这些网页开始抓取**parse():**一个方法当start_urls里面的网页抓取下来之后需要调用这个方法解析网页内容items定义抓取的字段名import scrapyclass MySpiderItem(scrapy.Item):# define the fields for your item here like:# name scrapy.Field()passpipeline数据存储的位置from itemadapter import ItemAdapterclass MySpiderPipeline:def process_item(self, item, spider):return itemsettings项目配置文件# Scrapy settings for my_spider project
#
# For simplicity, this file contains only settings considered important or
# commonly used. You can find more settings consulting the documentation:
#
# https://docs.scrapy.org/en/latest/topics/settings.html
# https://docs.scrapy.org/en/latest/topics/downloader-middleware.html
# https://docs.scrapy.org/en/latest/topics/spider-middleware.htmlBOT_NAME my_spiderSPIDER_MODULES [my_spider.spiders]
NEWSPIDER_MODULE my_spider.spiders# Crawl responsibly by identifying yourself (and your website) on the user-agent
USER_AGENT Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.5060.134 Safari/537.36 Edg/103.0.1264.71# Obey robots.txt rules
# robots协议默认遵守
ROBOTSTXT_OBEY False
# 日志等级ERROR最高
LOG_LEVEL ERROR# Configure maximum concurrent requests performed by Scrapy (default: 16)
# 并发请求(concurrent requests)的最大值
#CONCURRENT_REQUESTS 32# Configure a delay for requests for the same website (default: 0)
# See https://docs.scrapy.org/en/latest/topics/settings.html#download-delay
# See also autothrottle settings and docs# 下载延迟
#DOWNLOAD_DELAY 3
# The download delay setting will honor only one of:
# 对单个网站进行并发请求的最大值
#CONCURRENT_REQUESTS_PER_DOMAIN 16
# 对单个IP进行并发请求的最大值。如果非0则忽略
#CONCURRENT_REQUESTS_PER_IP 16# Disable cookies (enabled by default)
# COOKIES_ENABLED False# Disable Telnet Console (enabled by default)
#TELNETCONSOLE_ENABLED False# Override the default request headers:
# DEFAULT_REQUEST_HEADERS {
# Accept: text/html,application/xhtmlxml,application/xml;q0.9,*/*;q0.8,
# Accept-Language: en,
# }# Enable or disable spider middlewares
# See https://docs.scrapy.org/en/latest/topics/spider-middleware.html
#SPIDER_MIDDLEWARES {
# my_spider.middlewares.MySpiderSpiderMiddleware: 543,
#}# Enable or disable downloader middlewares
# See https://docs.scrapy.org/en/latest/topics/downloader-middleware.html
#DOWNLOADER_MIDDLEWARES {
# my_spider.middlewares.MySpiderDownloaderMiddleware: 543,
#}# Enable or disable extensions
# See https://docs.scrapy.org/en/latest/topics/extensions.html
#EXTENSIONS {
# scrapy.extensions.telnet.TelnetConsole: None,
#}# Configure item pipelines
# See https://docs.scrapy.org/en/latest/topics/item-pipeline.html
ITEM_PIPELINES {# 优先级 数字越小越靠近管道my_spider.pipelines.MySpiderPipeline: 300,
}# Enable and configure the AutoThrottle extension (disabled by default)
# See https://docs.scrapy.org/en/latest/topics/autothrottle.html
#AUTOTHROTTLE_ENABLED True
# The initial download delay
#AUTOTHROTTLE_START_DELAY 5
# The maximum download delay to be set in case of high latencies
#AUTOTHROTTLE_MAX_DELAY 60
# The average number of requests Scrapy should be sending in parallel to
# each remote server
#AUTOTHROTTLE_TARGET_CONCURRENCY 1.0
# Enable showing throttling stats for every response received:
#AUTOTHROTTLE_DEBUG False# Enable and configure HTTP caching (disabled by default)
# See https://docs.scrapy.org/en/latest/topics/downloader-middleware.html#httpcache-middleware-settings
#HTTPCACHE_ENABLED True
#HTTPCACHE_EXPIRATION_SECS 0
#HTTPCACHE_DIR httpcache
#HTTPCACHE_IGNORE_HTTP_CODES []
#HTTPCACHE_STORAGE scrapy.extensions.httpcache.FilesystemCacheStorage