[英]How to get innerHTML with Xpath javascript?
我需要從內部元素中檢索文本。 使用php,使用Xpath非常容易。 在javascript中呢? 我也需要從具有特定ID的特定元素中檢索。 以下代碼中的請求。 可以說CORS沒問題。 我安裝了chrome擴展程序來解決CORS。
<html>
<head>
</head>
<body>
<script>
fetch('https://alexistogel1.blogspot.com/2019/08/post-tak-penting.html')
.then(function (response) {
return response.text();
})
.then(function (html) {
var element = document.evaluate( "//div[@id='post-body-6034358444211110141']" ,document, null, XPathResult.ANY_TYPE, null );
console.log(element);
});
</script>
</body>
</html>
使用DOMParser
將您的字符串轉換為document
。
現在,您可以使用document
在getElementById
的幫助下選擇其中的元素。
fetch('https://alexistogel1.blogspot.com/2019/08/post-tak-penting.html')
.then(function (response) {
return response.text();
})
.then(function (text) {
const parser = new DOMParser();
return parser.parseFromString(text, 'text/html');
})
.then(function (html) {
var element = html.getElementById('post-body-6034358444211110141');
console.log(element.innerHTML);
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.