0

How to fetch info or a title from a website every time you use a command? For example, if I use a command called ?title I get a name of the title from a website, how should I do that?

1 Answers1

0
const fetch = require("node-fetch");
const cheerio = require("cheerio");

let url = "https://www.nike.com/de/";
    fetch(url).then(res => res.text())
      .then(html => {
        const parse = cheerio.load(html)
        console.log(parse("meta[property='og:title']")[0].attribs.content)
      })

You need node-fetch and cheerio, so make sure you've installed it. In my example I used the Nike website, so for your website/s it could be slightly different.

So first it fetches with node-fetch the url and displays the result as HTML. Then we use cheerio to read out the HTML and get the title of the website from it.

My example returns:

Nike Official Site
Jannik Schmidtke
  • 1,257
  • 2
  • 5
  • 15
  • It works, thanks! But what if i want to get something from id or class? –  Jan 07 '21 at 06:01