Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use DOM query methods against a Fetch API response?

I'm trying to automate interaction with an old web interface that currently only exposes user-driven forms, so I need to scrape some information from a web page with a dynamic request.

If I use an XHR, I can treat the response as a Document, which lets me use methods like querySelector to retrieve information from specific nodes. I'd like to try to use the Fetch API, though, which only gives me a Body. This has blob, formData, json, and text, but I don't see anything that would let me treat it as a Document.

Am I missing something? Can I get a Document or something else query-able directly from fetch? If not, is there a simple way to take a string (from Body.text()) and turn it into a Document?

like image 568
Coderer Avatar asked Dec 14 '17 08:12

Coderer


People also ask

How do I convert a fetch response to HTML?

The trick is to use the text() method instead of the json() method on the stream. This will return a text string of the HTML. fetch('/about'). then(function (response) { // The API call was successful!

Which method call is changed to handle a successful response returned by Fetch?

This is the first method called in our fetch() chain, if it resolves, we then call our json() method which again returns a Promise from the response. json() call. After this we have an object of the parsed JSON. If the parsing fails the Promise is rejected and the catch statement executes.

How do you get data from an API using Fetch?

Approach: First make the necessary JavaScript file, HTML file and CSS file. Then store the API URL in a variable (here api_url). Define a async function (here getapi()) and pass api_url in that function. Define a constant response and store the fetched data by await fetch() method.


2 Answers

Can I get a Document or something else query-able directly from fetch? If not, is there a simple way to take a string (from Body.text()) and turn it into a Document?

You can’t get a Document back directly from the Fetch API itself. But you can use DOMParser and give its parseFromString() method the text the Fetch Body.text() promise resolves to.

fetch("https://enable-cors.org/")
  .then(response => response.text())
  .then(text => {
    const parser = new DOMParser();
    const htmlDocument = parser.parseFromString(text, "text/html");
    const section = htmlDocument.documentElement.querySelector("section");
    document.querySelector("div").appendChild(section);
  })
<div></div>
like image 100
sideshowbarker Avatar answered Oct 01 '22 00:10

sideshowbarker


async function fetchDocument(url) {        
    let response = await fetch(url);
    let text = await response.text();
    if (!fetchDocument.parser) fetchDocument.parser = new DOMParser(); // the dom parser is reusable
    return fetchDocument.parser.parseFromString(text, "text/html");
}

let doc = await fetchDocument(urlToFetch)
like image 24
aljgom Avatar answered Oct 01 '22 01:10

aljgom