如何在现有 Scrapy 项目中用 scrapling_response 装饰器接入 Scrapling 解析 API
如何在现有 Scrapy 项目中用 scrapling_response 装饰器接入 Scrapling 解析 API【免费下载链接】Scrapling️ An adaptive Web Scraping framework that handles everything from a single request to a full-scale crawl!项目地址: https://gitcode.com/GitHub_Trending/sc/Scrapling如果你已经有一个 Scrapy 项目但希望爬虫里的解析代码改用 Scrapling 的 APIfind_by_text、find_similar、get_all_text等Scrapling 提供了专门的 Scrapy 集成把scrapling_response装饰器加到 spider 回调上回调里的response参数就会自动从 Scrapy 的响应对象变成 Scrapling 的 Response 对象抓取流程仍由 Scrapy 负责解析改由 Scrapling 负责不需要重写项目。准备条件一个已经在运行的 Scrapy 项目集成文档明确要求环境中装有 Scrapy。Scrapling 默认安装即可不需要任何 extraspip install scrapling根据 README 的说明Scrapling 要求 Python 3.10 或更高版本默认安装只包含解析引擎及其依赖正好覆盖这个集成所需的范围。给 spider 回调加上 scrapling_response 装饰器集成文档给出的完整示例如下可以按结构照搬到现有 spider 上start_urls和解析逻辑替换成你自己的import scrapy from scrapling.integrations.scrapy import scrapling_response class QuotesSpider(scrapy.Spider): name quotes start_urls [https://quotes.toscrape.com] scrapling_response def parse(self, response): # response is now a Scrapling Response first_quote response.find_by_text(The world as we have created it, partialTrue) for quote in [first_quote, *first_quote.find_similar()]: card quote.parent yield { text: quote.get_all_text(stripTrue), author: card.find(small, class_author).text, tags: [tag.text for tag in card.find_all(a, class_tag)], } next_page response.css(li.next a::attr(href)).get() if next_page: yield scrapy.Request(response.urljoin(next_page), callbackself.parse)加完装饰器后回调内部就能同时使用 Scrapling 的选择方式find_by_text、find_similar、get_all_text、find/find_all和原有的 CSS/XPath 选择方式response.css(...)仍然可用两种风格可以在同一个回调里混用。装饰器对回调类型的要求装饰器支持 Scrapy 的全部回调类型普通函数、生成器、协程和异步生成器。包装器会保留原回调的类型、函数名和 docstring所以 Scrapy 的回调内省introspection和 contracts 继续正常工作。可选向装饰器传 Selector 配置需要调整解析行为时把配置直接写在装饰器参数里它会转发给生成的Responsescrapling_response(adaptiveTrue, keep_commentsTrue) def parse_product(self, response): ...实现侧的 scrapling/integrations/scrapy.py 中列出的可转发配置包括huge_tree、keep_comments、keep_cdata、adaptive、storage、storage_args和adaptive_domain。回调之外用 convert_response 直接转换如果在中间件、管道等非回调位置拿到了 Scrapy 响应不用装饰器直接调用转换器from scrapling.integrations.scrapy import convert_response scrapling_response convert_response(scrapy_response, keep_commentsFalse, keep_cdataFalse)convert_response的第二个起参数同样是转发给Response构造函数的selector_config用法与参数化的装饰器一致。验证接入是否生效仓库自带的集成测试 tests/integrations/test_scrapy.py 展示了判断接入成功的检查方式可以照搬到自己的 spider 里from scrapling.engines.toolbelt.custom import Response scrapling_response def parse(self, response): assert isinstance(response, Response) # 装饰器生效后应为 True title response.css(title::text).get() yield {title: title}测试中还覆盖了几项可核对的行为转换后的Response上css/xpath选择、urljoin正常工作参数化配置生效例如传入keep_commentsTrue后xpath(//comment())能取到注释节点而默认转换时取不到Cookies 会被解析进response.cookies字典回调参数中没有 Scrapy 响应对象时包装器抛出TypeError信息为No Scrapy response found in the arguments of ...——看到这个报错说明装饰器被用在了一个不接收 Scrapy 响应的回调上。跑通scrapy crawl spider 名后在终端日志或 item 中确认解析结果来自 Scrapling 的选择方法即完成接入。边界与注意事项翻页请求仍要用 Scrapy 的方式发。文档明确要求yield scrapy.Request(response.urljoin(href))。Scrapling 的Response.follow()方法是为 Scrapling 自己的 spider 系统构建请求的Scrapy 无法识别其产物不能用在 Scrapy 回调里。meta是浅拷贝。转换时response.meta字典被浅拷贝一份其他中间件存入的对象仍可通过它访问。文档举例配合scrapy-playwright时页面对象仍在response.meta[playwright_page]。Cookies 从原始Set-Cookie头逐行解析。集成文档说明 cookies 是从原始Set-Cookie头解析进response.cookies字典的实现代码中的注释补充了原因——Scrapy 的to_unicode_dict会用逗号合并重复头破坏多条Set-Cookie。装饰器只转换ScrapyResponse类型的参数位置参数优先其次检查关键字参数找不到 Scrapy 响应就报上述TypeError。更多 Scrapy 集成细节可参考 docs/integrations/scrapy.md 与 Scrapling 解析主类文档。【免费下载链接】Scrapling️ An adaptive Web Scraping framework that handles everything from a single request to a full-scale crawl!项目地址: https://gitcode.com/GitHub_Trending/sc/Scrapling创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考