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

如何使用XPath选取嵌套div中特定段落文本对应的目标元素内容?

Got it, let's break down how to extract those target values from the HTML structure you described. Here's a step-by-step approach along with the extracted content:

1. Target Structure Breakdown

First, we zero in on the <div class="specifics"> container, which holds multiple <div class="row"> elements. Each row follows a consistent pattern:

  • A <div class="name"> wrapping a <p> tag with descriptive labels (like Light: or Type:)
  • An adjacent <div class="value"> that contains the actual content we need to extract.
2. Extracted Target Content

By matching the label text in the .name section, we pull the following relevant values:

  • For the label Light: → Part Sun, Sun
  • For the label Type: → Herb, Houseplant
3. Example Programmatic Extraction (JavaScript)

If you need to automate this extraction on the client side, here's a straightforward code snippet:

// Target the main specifics container
const specificsContainer = document.querySelector('.specifics');
const plantInfo = {};

// Iterate through each row to find matching labels
specificsContainer.querySelectorAll('.row').forEach(row => {
  const labelText = row.querySelector('.name p').textContent.trim();
  const valueText = row.querySelector('.value').textContent.trim();

  // Capture values for our target labels
  if (labelText === 'Light:') {
    plantInfo.lightRequirements = valueText;
  } else if (labelText === 'Type:') {
    // Split multi-value entries into a clean array
    plantInfo.plantTypes = valueText.split(',').map(item => item.trim());
  }
});

console.log(plantInfo);
// Output: { lightRequirements: "Part Sun, Sun", plantTypes: ["Herb", "Houseplant"] }

内容的提问来源于stack exchange,提问作者whale-b

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.26 09:37:36