如何使用Xpath抓取包含特定字符串的JSON



我有一个这样的HTML数据。

<script type="application/ld+json">{ "name": "apple", "price": 100 }</script>
<script type="application/ld+json">{ "name": "banana", "price": 200 }</script>
<script type="application/ld+json">{ "name": "orange", "price": 300 }</script>

如何抓取包含"香蕉"的Json数据?Xpath。

例如,下面的javascript代码可以抓取包含banana的JSON。但它只是抓取第二个JSON。
const htmlString = res;
const doc = new DOMParser();
const string = doc.parseFromString(htmlString, 'text/html');
const result = string.evaluate('//script[@type="application/ld+json"]', string, null, 6, null);
const character = result.snapshotItem(2);
console.log(character);

在下面的代码中,变量为Null。

const htmlString = res;
const doc = new DOMParser();
const string = doc.parseFromString(htmlString, 'text/html');
const result = string.evaluate('//script[contains(text(), "banana")]', string, null, 6, null);
const character = result.snapshotItem(1);
console.log(character);

目标图像为{"name": "banana", "price": 200}。

索引应该是0,因为您的目标正是您想要的。

const character = result.snapshotItem(0);

为什么使用xpath?

const obj = [...document.querySelectorAll("script[type='application/ld+json']")]
.map(script => JSON.parse(script.textContent))
.filter((item)=>item.name==="banana")

console.log(obj[0])
<script type="application/ld+json">{ "name": "apple", "price": 100 }</script>
<script type="application/ld+json">{ "name": "banana", "price": 200 }</script>
<script type="application/ld+json">{ "name": "orange", "price": 300 }</script>

你也可以这样做:

result = string.evaluate('//script[contains(text(), "banana")]/text()', string, null, 6, null),
character = result.snapshotItem(0).nodeValue;
console.log(character);

最新更新