Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use CHEERIO.js for this HTML?

<div class="A">
<section class="B" data-vr-zone="B">
    <header class="C"> BarFoo</header>
    <ul class="list">
        <li data-vr-contentbox=""> 
            <a href="http://www.foobar.com/.../html">
                <small>BarBar</small> 
                <span>Foo Bar foobarbar FooFoo?</span>
            </a>

        </li>
        <li data-vr-contentbox=""> 
            <a href="http://www.foofoobar.com/.../html">
                <small>BarBarBar</small> 
                <span>Foo foo FooFoo?</span>
            </a>

        </li>

I want to access the url in the HREF attribute. And the text in the SPAN -- Of only the first list item.

What I have works but I'm looking to learn a better way.

var url = $('div .A').children().children().children().children()[0].attribs.href;

var title = $('div .A').children().children().children().children()[0].children[2].children[0].data;

like image 514
georgecurioz Avatar asked Nov 18 '13 17:11

georgecurioz


People also ask

Can I use Cheerio in browser?

Cheerio is just a nodejs implementation of jQuery. Just use jQuery when you are scripting for web browser. You can't do that in a browser because of same origin policy.

What is Cheerio in JavaScript?

Cheerio js is a Javascript technology used for web-scraping in server-side implementations. Web-scraping is a scripted method of extracting data from a website that can be tailored to your use-case. NodeJS is often used as the server-side platform.

How do I select HTML element in node JS?

To select a <select> element, you use the DOM API like getElementById() or querySelector() . How it works: First, select the <button> and <select> elements using the querySelector() method. Then, attach a click event listener to the button and show the selected index using the alert() method when the button is clicked.

How do you get elements in Cheerio?

Cheerio get element attributesAttributes can be retrieved with attr function. import fetch from 'node-fetch'; import { load } from 'cheerio'; const url = 'http://webcode.me'; const response = await fetch(url); const body = await response. text(); let $ = load(body); let lnEl = $('link'); let attrs = lnEl.


1 Answers

You want to use a better selector string to target the element & attribute of interest. Exactly how vague or precise you go involves trade-offs of coupling too tighly to the DOM structure and thus some irrelevant change to the HTML means your selector doesn't match anymore or using too vague a selector and matching more stuff than you intend.

  • vaguest: 'a' (find every anchor)
  • '.A a' (every anchor inside the div class="A")
  • Recommended: '.A li a' (must be part of a list)
  • crazy specific: 'div.A section.B ul.list li a'

.

var link = $('.A li a');
var href = link.attr('href');
var spanText = link.find('span').first().text();
like image 133
Peter Lyons Avatar answered Sep 27 '22 16:09

Peter Lyons