我正在嘗試從結構不太好的 dd 組中獲取資料。“組”確實有一個 DD 包裝器,但它內部只有 p/div,沒有圍繞它的分組包裝器:
[DD]
[P] Key
[DIV]
[P] Value
[P] Key
[DIV]
[P] Value
是否有可能以正確的方式收集資料?
我正在處理的html代碼:
<dd class="product-specifications-v2__items">
<p class="product-specifications-v2__key">
EAN/UPC - product
</p>
<div class="product-specifications-v2__value">
<p class="product-specifications-v2__value-item">
7912372
</p>
</div>
<p class="product-specifications-v2__key">
Weight
</p>
<div class="product-specifications-v2__value">
<p class="product-specifications-v2__value-item">
2,170
<span>kg</span>
</p>
</div>
</dd>
我目前得到以下結果作為陣列:
{
"key": [
"EAN\/UPC - product",
"Weight"
],
"value": [
"7912372",
"2,170 kg",
]
}
我需要得到(沒有陣列):
{
"key": "EAN/UPC - product",
"value": "7912372"
},
{
"key": "Weight",
"value": "2,170 kg"
}
我使用以下請求通過 API 獲取資料:
{
"name":"attributes",
"selector":"div.product-specifications-v2__wrapper dl dd",
"targets":[
{
"name":"key",
"selector":"p.product-specifications-v2__key",
"dataType":"title"
},
{
"name":"value",
"selector":"div.product-specifications-v2__value p.product-specifications-v2__value-item",
"dataType":"title"
}
]
}
uj5u.com熱心網友回復:
使用 XPath 3.1(例如,在使用 Saxon-JS ( https://www.saxonica.com/saxon-js/documentation2/index.html ) 的瀏覽器中,也使用 Node),您可以使用創建 XPath 的路徑運算式3.1 XDM 映射與key和value:
//dd[@class = 'product-specifications-v2__items']/p[@class = 'product-specifications-v2__key']!map { 'key' : normalize-space(), 'value' : following-sibling::div[@class = 'product-specifications-v2__value'][1]!normalize-space() }
const html = `<dd >
<p >
EAN/UPC - product
</p>
<div >
<p >
7912372
</p>
</div>
<p >
Weight
</p>
<div >
<p >
2,170
<span>kg</span>
</p>
</div>
</dd>`;
var htmlDoc = new DOMParser().parseFromString(html, 'text/html');
const results = SaxonJS.XPath.evaluate(`//dd[@class = 'product-specifications-v2__items']/p[@class = 'product-specifications-v2__key']!map { 'key' : normalize-space(), 'value' : following-sibling::div[@class = 'product-specifications-v2__value'][1]!normalize-space() }`, htmlDoc, { 'xpathDefaultNamespace' : 'http://www.w3.org/1999/xhtml' });
console.log(results);
<script src="https://www.saxonica.com/saxon-js/documentation2/SaxonJS/SaxonJS2.rt.js"></script>
Saxon-JS 的 JavaScript API 將 XDM 映射序列作為 JSON 物件陣列回傳給 JavaScript。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/410090.html
標籤:
