How to get only specific page elements via Xios.get()?

Asked

Viewed 99 times

0

With the Axios.get method from the Axios.js library I can get an entire HTML page, right? Example:

  axios.get('http://www.umaurl.com')
                .then(resp => {
                    console.log(resp)

                })

But now, if I just want to search for some specific elements of that page, like some Ivs, how could I do that in a get request via Axios? You would have to pass a selector for example, on the object that is the second parameter of Axios.get, right? But how exactly would the syntax of this look like?

1 answer

1

You can use the Cheerio to do this.

 const cheerio = require('cheerio');
 const axios   = require('axio');

 axios.get('http://www.umaurl.com')
                .then(resp => {
                  $ = cheerio.load(resp.data);
                  var elemento = $('input[id="elemento"]') /// sintax do jquery
                  /// agora você faz o que quiser com o elemento
                })

  • Fine, I’ll try it first

  • Ah, but there’s one detail: I’m actually trying to give GET, not post. I want to give GET to external Urls, but they always refuse these Xmlhttprequest Axios requests. See this thread https://answall.com/questions/452749/como-chamar-e-obtervia-get-uma-p%C3%a1gina-html-external-using-Axios

  • 1

    I tested your code here and I didn’t get the same error

Browser other questions tagged

You are not signed in. Login or sign up in order to post.