1

How to get IMVALUE on source code like that with cheerio on node.js?

Element/View Source:

<div class="e7m mess_bodiyy">
  <p style="max-width: 600px">
    Hello x7907062441,
    <br />
    <br />
    This is the value:
    <br />
    <a href="http://mywebsite.com" target="_blank" rel="nofollow">
      IMVALUE
    </a>
    .<br />
    <br />
  </p>
</div>

I've tried with

const src = $('<div class="e7m mess_bodiyy"></div>').

But the result is undefined.

1 Answers1

0

Terminology technicality: that's text, not a value.

This is a value (it's a specific attribute):

<input value="some value">

(You can get that with $("input").val() or select it with $('[value="some value"]'))

This is text:

<p>some text</p>

(You get that with $("p").text())


You don't select and traverse CSS by passing in the HTML. If you wanted to do a substring check like that you can do so on plain strings, without Cheerio. But this isn't useful since in HTML, we want to disregard whitespace and the order of attributes. With strings, it's extremely difficult to determine which elements are ancestors of other elements. Cheerio parses the string into a tree that CSS selectors traverse, solving these problems.

Let's use Cheerio with a CSS selector on your string:

const cheerio = require("cheerio"); // 1.0.0-rc.12

const html = `<Your HTML>`;
const $ = cheerio.load(html);
console.log($(".e7m.mess_bodiyy a").text().trim()); // => IMVALUE

This selector says "find the <a> element that's some descendant of an element with the classes e7m and mess_bodiyy in any order".


Now, based on the discussion, if this doesn't log anything, maybe you're getting the HTML from the browser developer tools, which includes elements injected after page load by JavaScript. If you're doing a simple HTTP request to retrieve the static HTML, you may not see the same elements which are visible in dev tools. If this is the case, you'll need a different tool than Cheerio such as Puppeteer. See How can I scrape pages with dynamic content using node.js? for details.

ggorlen
  • 44,755
  • 7
  • 76
  • 106