narra_kk
narra_kk

Reputation: 111

Using cheerio to scrape a page won't work well

I have a page which looks like this:

                           <li title="Sulphurous" class='item q-440-5 q-440-border-5' data-tip="top" >
<a href="/effect/Sulphurous"><div class='item-icon' style='background-image:url(),url(/images/440/particles/64_94x94.png)'></div><div class='tag bottom-right'>avg 207.61</div><div class='tag top-left'>sulphurous</div></a>
<span style='display:none'>Unusual Sulphurous</span></li>



                            <li title="Purple Confetti" class='item q-440-5 q-440-border-5' data-tip="top" >
<a href="/effect/Purple%20Confetti"><div class='item-icon' style='background-image:url(),url(/images/440/particles/7_94x94.png)'></div><div class='tag bottom-right'>avg 57.39</div><div class='tag top-left'>p.fetti</div></a>
<span style='display:none'>Unusual Purple Confetti</span></li>

I want to extract title and tag bottom-right. The result should look like this:

name: Unusual Sulphurous
price: avg 207.61

name: Unusual Purple Confetti
price: avg 57.39

How do I extract avg 207.61 and avg 57.39 using cheerio?

I am only able to parse the title. Here's my script:

    $('li').each(function(){
        var name = ($(this).attr('title'));
        var price = ($(this).attr('tag bottom-right')); 

        nameList.push({
                  name: name,
                  price: price,
        });
    });

But the price is undefined. I tried tag_bottom-right, tag-bottom-right, tag.bottom-right and .tag.bottom-right but it still didn't work.

Also tried doing this:

    $("tag.bottom-right").each(function() {
        let id = ($(this).data("tag.bottom-right"));
     });

But still undefined. What am I doing wrong?

Upvotes: 0

Views: 51

Answers (1)

pguardiario
pguardiario

Reputation: 54992

You're not using the right css:

let prices = $(".tag.bottom-right").map((i, el) => $(el).text())

Upvotes: 1

Related Questions