You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Scrapy爬虫提取DSM站点商品信息失败求助

解决Scrapy爬取Dover Street Market页面生成空CSV的问题

核心问题排查

空CSV说明爬虫未匹配到任何目标元素,大概率是CSS选择器失效,也可能是页面动态加载导致原始响应无商品数据。先从选择器调试入手:

1. 用Scrapy Shell验证选择器有效性

先在终端启动Shell测试目标页面:

scrapy shell https://shop.doverstreetmarket.com/collections/shops-noah

依次执行以下命令,确认每个选择器是否能返回结果:

  • 检查商品容器是否存在:response.css('.product-item')(实际类名以页面HTML为准,常见还有.grid-item/.product-grid-item)
  • 提取商品名称:若a标签内嵌套span,直接用a::text会返回空,改用a.product-item__title::text(选所有子文本)或a.product-item__title span::text(直接选span文本),也可以用a.product-item__title.get().strip()自动拼接所有子节点文本
  • 提取价格:response.css('.product-item__price .price::text').get()
  • 提取链接:response.css('a.product-item__title::attr(href)').get(),再用response.urljoin(link)拼接完整URL

2. 修改爬虫脚本示例

针对你遇到的span嵌套问题,调整选择器后的完整脚本如下(以页面常见结构为例):

import scrapy

class NoahSpider(scrapy.Spider):
    name = 'noah_products'
    allowed_domains = ['shop.doverstreetmarket.com']
    start_urls = ['https://shop.doverstreetmarket.com/collections/shops-noah']

    def parse(self, response):
        # 遍历所有商品容器(以页面实际类名为准)
        for product in response.css('.product-item'):
            yield {
                # 提取名称:自动获取a标签下所有文本,包括span内容
                'name': product.css('a.product-item__title').get(default='').strip(),
                # 提取价格:直接定位价格元素
                'price': product.css('.product-item__price .price::text').get(default='').strip(),
                # 拼接完整商品链接
                'link': response.urljoin(product.css('a.product-item__title::attr(href)').get(default=''))
            }

3. 处理动态加载场景

如果Shell中response.body看不到商品数据,说明页面是JS动态渲染的:

  • 可以开启Scrapy的DOWNLOAD_DELAY并添加浏览器UA(在settings.py中配置):
    USER_AGENT = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36'
    DOWNLOAD_DELAY = 2
    
  • 若仍无效,可使用scrapy-playwright或scrapy-splash渲染JS页面,或抓包找商品数据的API接口(XHR请求中的JSON数据)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.03 20:00:04