我获取了一个HTML页面作为字符串,并希望检索标签之间的文本并从json键获取值。 在javascript中,这可行: 例如,如果字符串是:
<!doctype html>
<html lang="fr-FR">
<head>
<script>
window.changeTargetingData = {
"petition":{
"id":"19197290",
"signatureCount":{"total":323030,"displayed":323030}
}
};
</script>
</head>
</html>
这给了我我在javascript中想要的东西:
const doc = new DOMParser().parseFromString(textResponse, 'text/html');
const script = doc.querySelector('script');
const objJSON = script.textContent.match(/window.changeTargetingData = ([^]+);/)[1];
const obj = JSON.parse(objJSON);
console.log(obj.petition.signatureCount.total);
但是我没有在corvid中找到DOMParser或任何DOM请求。是否有任何东西可以从html页面获取值?
谢谢。
答案 0 :(得分:0)
好的,您只需要更改文档上写的内容即可。 是:
import { JSDOM } from 'jsdom';
const domm = new JSDOM(`<!DOCTYPE html><p>Hello world</p>`);
console.log(domm.window.document.querySelector("p").textContent);
现在可以使用了......