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

嵌套Div与表格结构下的XPath文本提取问题求助

Extracting PRODUCT_NAME from Each list_* Element Efficiently

Great question! Let's fix this so you can pull all those product names in order without manually looping through each list index. Here's how to do it right:

Optimal XPath Approach

Instead of targeting individual list_1, list_2, etc., we can write an XPath that selects all relevant list items and grabs the product title from each one.

Use this expression:

response.xpath('//ul[@class="listing_pages"]/li//a[@class="product_title"]/text()').extract()

Why this works:

  • //ul[@class="listing_pages"] narrows us down to the exact container holding your list items, so we don't accidentally pick up unrelated elements on the page.
  • /li selects every list item inside that container (all your list_* elements).
  • //a[@class="product_title"] finds the specific link with the product name inside each list item.
  • /text() extracts the actual product name text from each link.

This will return a list of product names in the same order they appear in the HTML.

Cleaner CSS Selector Alternative

If you prefer CSS selectors (they’re often more readable for simple structures), use this:

response.css('ul.listing_pages li a.product_title::text').extract()

Why this works:

  • ul.listing_pages targets the parent container of your list items.
  • li selects each list item within that container.
  • a.product_title picks the product title link inside each list item.
  • ::text extracts the text content of the link directly.

Why Your Previous Approaches Had Issues

  • Your first XPath was too broad (//*[@id="one"]//table//tr//h2//a[position()]//text()), which grabbed every <h2><a> element on the page instead of limiting to your target list items.
  • Your second approach (//*[@id="list_1"]//table//tr//h2//a//text()) worked but only targeted a single list item, requiring manual index incrementing which isn’t efficient or scalable.

Both solutions above avoid these problems by targeting the entire list container and iterating through its items automatically, giving you all the product names in one clean, efficient query.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.12 03:43:56