<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;
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.
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.
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.
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.
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.
'a'
(find every anchor)'.A a'
(every anchor inside the div class="A")'.A li a'
(must be part of a list)'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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With