You need to enable JavaScript to run this app. Scrapy爬取WooCommerce站点无结果:获取URL实现网页数据提取方法
优惠活动
大模型
产品
解决方案
定价
更多

Scrapy爬取WooCommerce站点无结果:获取URL实现网页数据提取方法

故障排查与代码修正

核心根因定位

  • 方法覆盖导致流程完全失效:类内重复定义了两个同名parse方法,Python类语法会直接丢弃前一个方法定义,仅保留最后定义的解析逻辑。原代码中负责列表页翻页、商品链接提取的逻辑被完全覆盖,实际运行时会直接用详情页解析逻辑处理起始的shop列表页,自然匹配不到任何字段,输出空结果。
  • 选择器语法完全失效:代码中大量混入了从网页源码复制带来的HTML转义字符,比如把双引号转成"、CSS层级分隔符>转成>,像::attr("href")、h3.product-title>a这类写法不符合Scrapy选择器语法,无法匹配任何DOM节点。
  • 采集链路错位:没有遵循Scrapy的异步请求流程,原逻辑试图在列表页直接提取仅存在于商品详情页的独立字段,从匹配逻辑上就不可能获取到有效内容。
  • 采集量不足:列表页提取商品链接时仅调用.get()获取首个元素,单页12个商品仅能捕获1个,和预期684个商品的采集目标存在量级差距。

修正后的完整可运行代码

import scrapy

class BlogSpider(scrapy.Spider):
    name = 'blogspider'
    start_urls = [
        'https://perfumehut.com.pk/shop/',
    ]

    # 列表页解析器:承担商品详情链接提取、分页遍历职责
    def parse(self, response):
        # 遍历当前页所有商品卡片节点
        product_cards = response.css('div.product-grid-item')
        for card in product_cards:
            # 提取单个商品的详情页链接
            product_url = card.css('a.product-image-link::attr(href)').get()
            if product_url:
                # 构造详情页异步请求,交由专门的详情页解析方法处理
                yield scrapy.Request(
                    url=product_url,
                    callback=self.parse_detail
                )

        # 提取分页导航中的下一页链接,递归遍历所有列表页
        next_page = response.css('ul.page-numbers > li > a.next.page-numbers::attr(href)').get()
        if next_page:
            self.logger.info(f"正在遍历列表页: {next_page}")
            yield scrapy.Request(url=next_page, callback=self.parse)

    # 商品详情页解析器:承担单商品全量字段提取职责
    def parse_detail(self, response):
        attributes = response.css('td.woocommerce-product-attributes-item__value p::text').getall()
        yield {
            'title': response.css('h1::text').get(),
            # 增加列表长度判断,避免属性项不足时出现下标越界中断采集
            'batt': attributes[3] if len(attributes) > 3 else None,
            'brand': response.css('div.woodmart-product-brand img::attr(alt)').get(),
            'brandimg': response.css('div.woodmart-product-brand img::attr(src)').get(),        
            'price': response.css('p.price').xpath('./span/bdi/text()').get(),
            'r-price': response.css('p.price').xpath('./del/span/bdi/text()').get(),
            's-sale': response.css('p.price').xpath('./ins/span/bdi/text()').get(),
            'breadcrumbs': response.css('nav.woocommerce-breadcrumb a::text').getall(),
            'tags': response.css('span.tagged_as a::text').getall(),
            'attributes': attributes,
            'img': response.css('figure.woocommerce-product-gallery__image a::attr(href)').getall(),
            'description': response.css('div.woocommerce-product-details__short-description p::text').get(),
            'description1': response.css('#tab-description > div > div > p::text').getall(),
            'description2': response.css('#tab-description > div > div > div > div > div > div > div > p::text').getall()
        }

运行提示

  • 首次测试建议调低并发参数,通过命令scrapy crawl blogspider -s CONCURRENT_REQUESTS=2 -o products.json运行,避免触发站点防护机制
  • 若出现字段提取为空的情况,可通过scrapy shell 对应商品链接进入交互式调试环境,实时调整CSS/XPath匹配规则
  • 上述代码会自动完成57个列表页的递归遍历,自动捕获单页12个商品的详情链接,无需手动拼接分页URL,即可覆盖预期684个商品的采集目标

内容的提问来源于stack exchange,提问作者Hannah James

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.11.12 22:06:10