0

I use node-fetch , and I get the body of the site this way:

import fetch from 'node-fetch';

(async () => {
    const response = await fetch('link');
    const body = await response.text().

    console.log(body);
})()

The console displays the full body of the entire page. But I want to get a specific element with a certain class. How do I change the code to do this?

John
  • 33
  • 7
  • 1
    `he console displays the full body` - of course it does ... you've done NOTHING to achieve your goal ... you'll need a DOM to make it easy ... otherwise ... code to search for whatever you want – Bravo Jul 02 '22 at 10:04
  • 1
    @John [cheerio](https://www.npmjs.com/package/cheerio) is your friend – moonwave99 Jul 02 '22 at 10:07
  • 1
    There is a [dom-parser](https://www.npmjs.com/package/dom-parser) package available for node, which provides similar functionality to the browsers [DOMParser API](https://developer.mozilla.org/en-US/docs/Web/API/DOMParser). – Christopher Jul 02 '22 at 10:08

2 Answers2

1

You can use cheerio.js. It is an implementation of jQuery for node.

The below code selects an h2 and changes its text to Hello World.

const cheerio = require('cheerio');
const $ = cheerio.load(body);

$('h2').text('Hello World!');
Khalil
  • 1,495
  • 6
  • 14
0

Well, you've already achieved obtaining a string that contains the entire html of the page.

Now you can write code that extracts the part you want from that string. You said you're wanting to extract an element that has a particular class.

As moonwave99 commented, you could use cheerio, or some other html parser to extract the element with the class name you're targeting.

Or, if you want to avoid using an outside package, you could just write a regular expression to match the HTML element you want from that html string:

let wholeHTML =
`<html>
<p>Paragraph 1</p>
<p class="classIwant">Paragraph 2</p>
<p>Paragraph 3</p>
</html>`;

let rx = /<p class="classIwant".*\/p>/gm;
let matches = wholeHTML.match(rx);
console.log(matches[0]);
Lonnie Best
  • 9,936
  • 10
  • 57
  • 97