Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

href retrieval with cheerio

Tags:

I have a downloaded html file that looks something like this

<html class="theme_">
<head>
<body>
    <div id="ad_1"></div>
    ...
    <div id="wrapper">
        <div id="top" style="height:11px;color:white;font-size:9px;font-weight:bold;"> </div>
        ...
        <div id="content" style="border-top:none;">
            ...
            <table id="user_list">
                <tbody>
                    <tr class="trodd">
                        <td width="10%" valign="center" align="center">
                        <td class="list_art" style="width:160px;">
                        <td class="main_entry">
                            <h4>
                            <h5>
                                <a class="list_album" href="https://rateyourmusic.com/release/single/electra__ita_/feels_good__carrots_and_beets_/" title="[Album833409]">Feels Good (Carrots & Beets)</a>
                                <span class="rel_date">(1982) [Single]</span>
                                </h5>
                            </td>
                            <td></td>
                        </tr>
                        <tr class="treven">

I need to get to each of the <tr class="trodd"> and <tr class="treven"> entries of the table user_list and retrieve the href. This is the code I have now and it returns just an empty array. One of the things I can't get a grasp on is whether cheerio is capable of finding each class="list_album" like this or you have to make your way down the file's hierarchy with a bunch of $( )'s.

var cheerio = require("cheerio");
var file = "...path...";
var links = [];

var $ = cheerio.load(file);
$('list_album').each( function () {
    var link = $(this.attr('href'));
    links.push({"link": link});
});

console.log(links);