I'm trying to use the DOMParser to parse a string of html as a document object. This is what I've done:
<code>
let parser = new DOMParser();
let url = document.getElementsByClassName("a-link-emphasis")[0].href;
fetch(url)
.then(response => response.text())
.then(data => {
let doc = parser.parseFromString(data, "text/xml");
console.log(doc);
})
.catch(function(error) {
console.log(error);
});
</code>
I parse the response from the url as text so that the data variable is equal to the html source code of the page specified. It should be equal to a stringified version of this page's source code. This is the console.log of the data variable: image.
However when I try to parse this string with the parseFromString method, the variable doc is equal to the following html elements
<html xmlns="http://www.w3.org/1999/xhtml"><body><parsererror style="display: block; white-space: pre; border: 2px solid #c77; padding: 0 1em 0 1em; margin: 1em; background-color: #fdd; color: black"><h3>This page contains the following errors:</h3><div style="font-family:monospace;font-size:12px">error on line 1 at column 2: StartTag: invalid element name
</div><h3>Below is a rendering of the page up to the first error.</h3></parsererror></body></html>
Does anyone know what could be doing this? Thanks very much for your help.
console.log()
the text before trying to parse it? That's where the error is.