当前位置: 首页 > news >正文

最经济 网站建设合肥网络推广公司

最经济 网站建设,合肥网络推广公司,织梦 网站模板,网站演示网站代码爬虫工作量由小到大的思维转变---<第二十六章 Scrapy通一通中间件的问题>-CSDN博客 前言: (书接上面链接)自定义中间件玩不明白? 好吧,写个翻译的文档点笔记,让中间件更通俗一点!!! 正文: 全局图: 爬虫中间件--->翻译笔记: from scrapy import s…

爬虫工作量由小到大的思维转变---<第二十六章 Scrapy通一通中间件的问题>-CSDN博客

前言:

(书接上面链接)自定义中间件玩不明白? 好吧,写个翻译的文档+点笔记,让中间件更通俗一点!!!

正文:

全局图:

爬虫中间件--->翻译+笔记:

from scrapy import signals# useful for handling different item types with a single interface
from itemadapter import is_item, ItemAdapterclass XXXSpiderMiddleware:# Not all methods need to be defined. If a method is not defined,# scrapy acts as if the spider middleware does not modify the# passed objects.#--翻译-># 并非所有方法都需要被定义。如果某个方法没有被定义,# Scrapy 将会假设蜘蛛中间件不会修改传递的对象。@classmethoddef from_crawler(cls, crawler):# This method is used by Scrapy to create your spiders.#这个方法被用于创建你的Scrapy蜘蛛。s = cls()crawler.signals.connect(s.spider_opened, signal=signals.spider_opened)return sdef process_spider_input(self, response, spider):# Called for each response that goes through the spider# middleware and into the spider.# Should return None or raise an exception.# --翻译--># 这个方法由Scrapy用于创建你的蜘蛛。# 对于每个通过蜘蛛中间件到达蜘蛛的响应,会被调用。# 应该返回None或引发异常。# --->笔记:可以用它# 预处理响应数据,例如解析、提取信息、清洗数据等;# 对响应进行过滤或过程控制;# 处理错误或异常情况;# 添加自定义的功能或逻辑。# 如果你不希望对响应进行任何修改或处理,你可以简单地返回None。return Nonedef process_spider_output(self, response, result, spider):# Called with the results returned from the Spider, after# it has processed the response.# Must return an iterable of Request, or item objects.#---翻译-->该方法在蜘蛛处理响应并返回结果后被调用。# 必须返回一个可迭代的Request对象或item对象。'''笔记:这个方法允许你从蜘蛛处理的结果中进一步操作和处理数据。你可以修改结果,添加、删除或筛选特定的数据,或者在结果中创建新的Request对象来进行进一步的爬取。你可以通过yield语句将处理后的结果返回。总之,process_spider_output方法提供了一个在蜘蛛处理响应结果后对结果进行额外处理的机会,用于进一步定制和控制爬取过程。'''for i in result:yield idef process_spider_exception(self, response, exception, spider):# Called when a spider or process_spider_input() method# (from other spider middleware) raises an exception.# Should return either None or an iterable of Request or item objects.'''翻译:当蜘蛛或 process_spider_input() 方法(来自其他爬虫中间件)引发异常时调用。应该返回 None 或者一个可迭代的 Request 或 item 对象。笔记:1.用于处理当蜘蛛或其他爬虫中间件的 process_spider_input() 方法引发异常时的情况2.你可以针对异常情况进行任何处理。可以根据具体需求进行错误处理、记录日志、重新发送请求等操作。-->通俗地说,这个方法允许你在蜘蛛或其他爬虫中间件的输入方法引发异常时进行自定义处理。你可以根据具体的异常情况进行相应的处理操作,如重新发送请求、记录日志等。'''passdef process_start_requests(self, start_requests, spider):# Called with the start requests of the spider, and works# similarly to the process_spider_output() method, except# that it doesn’t have a response associated.# Must return only requests (not items).'''翻译: 在蜘蛛开始请求时被调用,与 process_spider_output() 方法相似,不同之处在于它没有与之关联的响应。笔记:这个方法允许你在蜘蛛开始请求之前对初始请求进行自定义处理。你可以修改请求对象的属性,添加额外的请求,或者根据需求生成新的请求对象--->在爬虫开始请求之前提供了一个自定义处理初始请求的机会,用于修改请求参数或生成新的请求对象。'''for r in start_requests:yield rdef spider_opened(self, spider):'''翻译:在爬虫开始运行时被调用;笔记:你可以用它1.初始化一些资源或状态;2.打开数据库连接或文件;3.设置爬虫的日志输出。..等--->可以在这里进行一些准备工作,以确保爬虫在运行时具备必要的环境和配置'''spider.logger.info("Spider opened: %s" % spider.name)

下载中间件--->翻译+笔记:

class JihaiEndDownloaderMiddleware:# Not all methods need to be defined. If a method is not defined,# scrapy acts as if the downloader middleware does not modify the# passed objects.# -->翻译:# 并非所有方法都需要被定义。如果某个方法没有被定义,# # Scrapy 将会假设下载中间件不会修改传递的对象。@classmethoddef from_crawler(cls, crawler):# This method is used by Scrapy to create your spiders.# 该方法由Scrapy用于创建你的爬虫。s = cls()crawler.signals.connect(s.spider_opened, signal=signals.spider_opened)return sdef process_request(self, request, spider):# Called for each request that goes through the downloader# middleware.# Must either:# - return None: continue processing this request# - or return a Response object# - or return a Request object# - or raise IgnoreRequest: process_exception() methods of#   installed downloader middleware will be called'''# 对每个通过下载中间件的请求进行处理。# 必须返回以下之一:# - 返回 None:继续处理该请求# - 或返回一个 Response 对象# - 或返回一个 Request 对象# - 或引发 IgnoreRequest:将调用已安装的下载中间件的 process_exception() 方法'''return Nonedef process_response(self, request, response, spider):# Called with the response returned from the downloader.# Must either;# - return a Response object# - return a Request object# - or raise IgnoreRequest# print(f'中间件接收:{response.text}')'''# 对从下载器返回的响应进行处理。# 必须返回以下之一:# - 返回一个 Response 对象# - 或返回一个 Request 对象# - 或引发 IgnoreRequest# print(f'中间件接收:{response.text}')'''return responsedef process_exception(self, request, exception, spider):# Called when a download handler or a process_request()# (from other downloader middleware) raises an exception.# Must either:# - return None: continue processing this exception# - return a Response object: stops process_exception() chain# - return a Request object: stops process_exception() chain'''# 当下载处理程序或 process_request() 方法(来自其他下载中间件)引发异常时调用。# 必须返回以下之一:# - 返回 None:继续处理该异常# - 返回一个 Response 对象:停止 process_exception() 链# - 返回一个 Request 对象:停止 process_exception() 链'''passdef spider_opened(self, spider):spider.logger.info("Spider opened: %s" % spider.name)

from_crawler(cls, crawler) 方法:

  • 该方法被 Scrapy 用于创建下载中间件实例。
  • 通常用于进行初始化操作和设置信号(Signal)。
  • 推荐用于在下载中间件创建时进行一些必要的准备工作或设置。

process_request(self, request, spider) 方法:

  • 对每个经过下载中间件的请求进行处理。
  • 必须返回以下之一:
  • 返回 None:继续处理该请求

-1.继续处理该请求,将会继续传递给后续的下载中间件处理,直到请求被发送到下载器。
-2.后续的下载中间件将有机会进一步处理请求或对请求进行修改。

  • 或返回一个 Response 对象

-1.将会终止后续的下载中间件的处理,并将该响应传递回爬虫进行处理(即交给响应处理函数)。
-2.后续的下载中间件的 process_request 和 process_response 方法不会再被调用。

  • 或返回一个 Request 对象

-1.将会终止后续的下载中间件的处理,并将该请求重新发送到引擎进行处理。
-2.该请求会经过中间件的处理流程,包括其他的下载中间件。
-3.可以用于对请求进行修改或生成新的请求来重新发起爬取。

  • 或引发 IgnoreRequest,将调用已安装的下载中间件的 process_exception() 方法。

-1.将会停止处理该请求,并调用已安装的其他下载中间件的 process_exception 方法。
-2.这是一个特殊情况,用于处理特定的异常情况或错误。
-3.通常用于处理某个请求无法继续处理的情况,可以选择忽略该请求,或者在 process_exception 方法中进行处理。

  • 用于自定义处理请求的行为,例如添加通用的请求信息、修改请求参数等。

--->补充:   (如果请求有问题)你想让一个request彻底从队列中消失,减少他接下来的生命周期;最直接的办法是引发异常; 如果返回了None他还是会传给spider的;关键取决于你怎么处理你的异常(要不要记录,在哪里记录); 最直接的还是给他引发异常~这样他就直接用日志记录了,不用再脱了裤子放屁跑spider里面去报异常!


process_response(self, request, response, spider) 方法:

  • 对从下载器返回的响应进行处理。
  • 必须返回以下之一:
  • 返回一个 Response 对象

-1.继续处理该响应,将其传递给后续的下载中间件进行处理。
-2.后续的下载中间件将有机会对响应进行进一步处理或对其进行修改。
-3.当没有其他中间件对响应进行进一步处理时,将会将响应传递给爬虫进行处理(即交给响应处理函数)。

  • 或返回一个 Request 对象

-1.将会将该请求重新发送到引擎进行处理,并再次经过下载中间件的处理流程。
-2.可以用于对响应进行处理后生成新的请求,或根据响应内容进行重定向等操作。

补充:重试用它;最好再+一个队列优先级,让这个重试的跑队列前面去,快点消掉关于他附带的(例如item)数据;然后还要注意别让这个请求被重复的url给筛掉了;

案例说明:(`+优先级`和`不去重属性`)

from scrapy import Requestclass RetryMiddleware:def process_response(self, request, response, spider):# 如果返回的响应状态码不是200,则进行重试if response.status != 200:# 修改原始的请求,并设置新的优先级和去重属性retry_request = request.copy()retry_request.priority += 1retry_request.dont_filter = True  # 设置不进行去重return retry_requestreturn response
  • 或引发 IgnoreRequest。
from scrapy.exceptions import IgnoreRequestdef process_response(self, request, response, spider):if response.status >= 400:raise IgnoreRequest  # 引发 IgnoreRequest 异常return response

引发 scrapy.exceptions.IgnoreRequest 异常,那么在引发异常后,Scrapy 将会终止当前请求的处理,并跳过后续的中间件和处理流程。--->通俗点:在 Scrapy 中,引发 scrapy.exceptions.IgnoreRequest 异常后,请求将不会传递给后续的中间件和爬虫进行处理。相当于告诉 Scrapy 忽略该请求,并继续处理下一个请求。     (----12月27日补)

  • 用于自定义处理响应的行为,例如解析、提取数据等。


process_exception(self, request, exception, spider) 方法:

  • 当下载处理程序或其他下载中间件的 process_request() 方法引发异常时调用。
  • 必须返回以下之一:
  • 返回 None:继续处理该异常

-1.如果希望继续处理异常并将其传递给其他下载中间件来处理,可以在 process_exception 方法中返回 None,让异常继续传递给后续的处理程序。

补:能给出来异常,这时候最好给他加个记录;然后返回None完事了!

案例:

class CustomDownloaderMiddleware:def process_exception(self, request, exception, spider):# 其他处理代码...# 获取异常的URLurl = request.url# 使用日志记录器记录异常的URLspider.logger.error(f"这个鬼url有问题,咱不要了;给他记一笔: {url}")return None

  • 返回一个 Response 对象:停止 process_exception() 链
  • 返回一个 Request 对象:停止 process_exception() 链

补:这俩在这方法里,我一般是不搁这处理的;你要自定义玩法,随你...就是用来重试和返回个别的给spider或者队列的

  • 用于自定义处理异常情况,例如处理超时、处理错误状态码等。

spider_opened(self, spider) 方法:

补:--->每次请求开一个spider?怕是有病吧.鸡肋的设计;直接去爬虫中间件里面整这玩意,这里直接给他过掉~


文章转载自:
http://dinncotranspecific.ssfq.cn
http://dinncogeneva.ssfq.cn
http://dinncodull.ssfq.cn
http://dinncolusus.ssfq.cn
http://dinncosmalto.ssfq.cn
http://dinncoundouble.ssfq.cn
http://dinncolimpkin.ssfq.cn
http://dinncobeanpole.ssfq.cn
http://dinncobayonet.ssfq.cn
http://dinncoforgot.ssfq.cn
http://dinncorhonchus.ssfq.cn
http://dinncochiba.ssfq.cn
http://dinncomight.ssfq.cn
http://dinnconidicolous.ssfq.cn
http://dinncosinlessly.ssfq.cn
http://dinncogossoon.ssfq.cn
http://dinncoestrade.ssfq.cn
http://dinncofermentor.ssfq.cn
http://dinncooverspecialization.ssfq.cn
http://dinncololiginid.ssfq.cn
http://dinncoburgeon.ssfq.cn
http://dinncohandwrought.ssfq.cn
http://dinncomadame.ssfq.cn
http://dinncobiomedicine.ssfq.cn
http://dinncoextrasolar.ssfq.cn
http://dinncogypster.ssfq.cn
http://dinncoromanza.ssfq.cn
http://dinncopyramidal.ssfq.cn
http://dinncoxenophobic.ssfq.cn
http://dinncophi.ssfq.cn
http://dinncoactinomorphic.ssfq.cn
http://dinncodisyllable.ssfq.cn
http://dinncohardtack.ssfq.cn
http://dinncomaghrib.ssfq.cn
http://dinncoafterburner.ssfq.cn
http://dinncohyposthenic.ssfq.cn
http://dinncosulphydryl.ssfq.cn
http://dinncolipizzaner.ssfq.cn
http://dinncorejoin.ssfq.cn
http://dinncosilverly.ssfq.cn
http://dinncosoekarno.ssfq.cn
http://dinncodamnify.ssfq.cn
http://dinncoalabastrine.ssfq.cn
http://dinncowhammy.ssfq.cn
http://dinncocholecystostomy.ssfq.cn
http://dinncocleanout.ssfq.cn
http://dinncoscarabaeus.ssfq.cn
http://dinncophotometer.ssfq.cn
http://dinncoundergarment.ssfq.cn
http://dinncorodney.ssfq.cn
http://dinncotantalum.ssfq.cn
http://dinncoswab.ssfq.cn
http://dinncotheodicean.ssfq.cn
http://dinncokith.ssfq.cn
http://dinncolansign.ssfq.cn
http://dinncoangolan.ssfq.cn
http://dinncocynegetics.ssfq.cn
http://dinncoluxuriance.ssfq.cn
http://dinncoscam.ssfq.cn
http://dinncohaggadist.ssfq.cn
http://dinncodapping.ssfq.cn
http://dinncoknives.ssfq.cn
http://dinncortol.ssfq.cn
http://dinncosuttee.ssfq.cn
http://dinncoaccepter.ssfq.cn
http://dinncopalafitte.ssfq.cn
http://dinncodichasium.ssfq.cn
http://dinncoelectrolysis.ssfq.cn
http://dinncostumblingly.ssfq.cn
http://dinnconowaday.ssfq.cn
http://dinncomaterialism.ssfq.cn
http://dinncoseattle.ssfq.cn
http://dinncoleucine.ssfq.cn
http://dinncohashery.ssfq.cn
http://dinncopasticheur.ssfq.cn
http://dinncofracturation.ssfq.cn
http://dinnconeuroleptanalgesia.ssfq.cn
http://dinncobrowbeat.ssfq.cn
http://dinncovideophone.ssfq.cn
http://dinncooppilate.ssfq.cn
http://dinncoteapot.ssfq.cn
http://dinncobehaviourist.ssfq.cn
http://dinncolibrary.ssfq.cn
http://dinncoresuscitable.ssfq.cn
http://dinncodelustre.ssfq.cn
http://dinncoinedible.ssfq.cn
http://dinncochivy.ssfq.cn
http://dinncosociopathic.ssfq.cn
http://dinncointergrade.ssfq.cn
http://dinncovinegar.ssfq.cn
http://dinncohypo.ssfq.cn
http://dinncoantacid.ssfq.cn
http://dinncoapolipoprotein.ssfq.cn
http://dinncoclayware.ssfq.cn
http://dinncogelidity.ssfq.cn
http://dinncohistorical.ssfq.cn
http://dinncocyme.ssfq.cn
http://dinncohereditary.ssfq.cn
http://dinncoshamo.ssfq.cn
http://dinncolanyard.ssfq.cn
http://www.dinnco.com/news/99250.html

相关文章:

  • 做类似慕课网的网站要多少钱宁波seo网络推广咨询热线
  • 漳州网站建设回忆互联客服QQ做一个公司网页多少钱
  • 网站建设流程报价我想做网络推广找谁
  • 网站页脚设计代码百度商家入驻
  • 用顶级域名做网站好吗饥饿营销案例
  • 深圳网站建设合同范本长春最专业的seo公司
  • android开发培训南京seo关键词排名
  • 内蒙建设信息网站营销策略怎么写模板
  • 国外网站建设现状图分析西安百度推广外包
  • 杭州网站建设公司哪家好郑州seo培训班
  • 如何做招聘网站的对比软文写作技巧及范文
  • php 网站开发贵阳网站优化公司
  • seo网络营销的技术seo网页优化培训
  • 北京哪个公司做网站好免费网站制作教程
  • 网站建设系统网站自助建站系统seo学习论坛
  • 重庆网站设计找重庆最佳科技一键搭建网站工具
  • 公司网站制作苏州广告策划案优秀案例
  • wordpress doc预览北京seo不到首页不扣费
  • wordpress 用户日志网站seo提升
  • 信宜网站建设公司东莞优化怎么做seo
  • 做网站用新域名还是老域名顶尖文案
  • 学信网 的企业网站给你做认证艾瑞指数
  • 网站开发技术论文重庆网站建设
  • php与网站建设win10必做的优化
  • 做物流哪个网站货源多网站seo外包公司有哪些
  • 做网站销售有前景推广策略
  • 锟鹏建设招聘网站全球最大的磁力搜索引擎
  • 域名注册以后会给你一个账户名密码上传做好的网站代运营电商公司
  • 网站界面设计的基本原则是什么下载谷歌浏览器
  • 中国贸易网站有哪些表白网站制作