How to get information in a text?

Asked

Viewed 96 times

0

would like to search and extract a string that is between tags. Using Javascript only with Node.

Example: Extract information inside any tag and save in some variable.

I don’t know how to implement a code for this. I don’t know if I could be clear.

Example 2 is what I’m trying to do. Search and save the names of the teachers that are on this page. I realized that all are among the header2 tags...

    const url = 'http://www.ppg-educacao.uff.br/novo/index.php/corpo-docente'
const axios = require('axios')

axios.get(url).then(response =>{
    const funcionarios = response.data
    console.log(funcionarios) //Somente para verificar as informações da página foram extraidas.
    //Eu gostaria de salvar o nome de todos os professores, e percebi que todos estão entre a tag <h2>...
    const tag = funcionarios.querySelector("h2")
    const conteudoDeTextoDaTag = tag.textContent
    console.log(conteudoDeTextoDaTag)
});
  • Hi Daniel, search on function substring and on regular Expressions that can help you. Also, post in question your code, what you have already done

  • Give a practical example of this to facilitate understanding. The way you described the question is vague

  • I’ll make it better.

  • Yes, try to improve and give examples. If it is HTML you can use a parser. If it is XML is already another kind of library.

  • I played an amendment there, I hope it became clearer...

  • In that case the const funcionarios is an array of H2? if it is, you can iterate over it to pick up each element.

  • No, in this case, this constant receives all the html code of the page.

  • I edited my answer down there. See if it helps you out.

Show 3 more comments

3 answers

0

0


Consider using a parser HTML like the Cheerio. For your example would look like:

const cheerio = require('cheerio');
const $ = cheerio.load(response.data);

console.log($('h2').text());

With it you can use selectors CSS to extract relevant information from HTML in question.

  • Your example helped me adapt to what I needed.

0

Many thanks to all for the time offered to this doubt and for the knowledge passed to me. Grateful and the question was resolved by Sorack’s reply.

Browser other questions tagged

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