Reputation: 349
I need to retrieve text from inner element. With php it so easy to use Xpath. how about in javascript? I need to retrieve from specific element with specific id too. the request under code below. Let say it doesnt problem with CORS. i installed chrome extension to solve 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>
Upvotes: 0
Views: 865
Reputation: 20944
Use a DOMParser
to convert your string to a document
.
Now you can use the document
to select the element inside it with the help of 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);
});
Upvotes: 1