最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

dom - Why doesn't javascript parse HTML from fetch? - Stack Overflow

programmeradmin0浏览0评论

I'm creating a chrome extension that scrapes photos from various websites. I have the URLs of each website and I want to fetch and parse those websites, and then grab specific img elements by class or ID.

I'm using the Javascript fetch() function together with DOMParser to parse the HTML and it returns the Document successfully, but any DOM query returns empty NodeLists / HTMLCollections.

fetch("www.example")
.then(res => {
    const parser = new DOMParser();
    const htmlDoc = parser.parseFromString(res, "text/html");
    console.log(htmlDoc.querySelectorAll('h1'))
})

This gives me an empty NodeList. Why?? And how can I get the elements I'm looking for?

I'm creating a chrome extension that scrapes photos from various websites. I have the URLs of each website and I want to fetch and parse those websites, and then grab specific img elements by class or ID.

I'm using the Javascript fetch() function together with DOMParser to parse the HTML and it returns the Document successfully, but any DOM query returns empty NodeLists / HTMLCollections.

fetch("www.example.")
.then(res => {
    const parser = new DOMParser();
    const htmlDoc = parser.parseFromString(res, "text/html");
    console.log(htmlDoc.querySelectorAll('h1'))
})

This gives me an empty NodeList. Why?? And how can I get the elements I'm looking for?

Share Improve this question asked May 31, 2019 at 19:30 maxmax 611 silver badge3 bronze badges 4
  • 10 I think you need to do res.text() or res.body(). Check the Fetch Response documentation. – user47589 Commented May 31, 2019 at 19:33
  • 1 The point Amy is making is that you are attempting to parse a DOM out of a Response object rather than the text returned from the server. – Randy Casburn Commented May 31, 2019 at 19:38
  • @RandyCasburn Whats the difference? And what would the solution look like? – max Commented May 31, 2019 at 19:42
  • @max - please see the answer – Randy Casburn Commented May 31, 2019 at 19:48
Add a ment  | 

1 Answer 1

Reset to default 8

You'll need to parse the response before you use it. Like this:

fetch("//randycasburn.")
  .then(res => res.text())
  .then(html => {
    const parser = new DOMParser();
    const htmlDoc = parser.parseFromString(html, "text/html");
    console.log(htmlDoc.querySelector('span').textContent)
  })

Otherwise by passing res itself a DOM will be created and res will be casted to a string ([object Response]), which results in this DOM: (note no H1)

<html>
  <head></head>
  <body>[object Response]</body>
</html>
发布评论

评论列表(0)

  1. 暂无评论