用cheerio检索href [英] href retrieval with cheerio
问题描述
我有一个下载的html文件看起来像这样
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">
我需要到达每个< tr class =trodd >
和< tr class =treven>
表格的条目 user_list
并检索href。这是我现在的代码,它只返回一个空数组。我无法理解的一件事是,cheerio是否能够找到这样的每个 class =list_album
,或者你必须按照文件的方式进行操作一堆 $()
的层次结构。
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);
推荐答案
我没有足够的声誉发表评论,所以我会在这里回答。
这似乎是一个错字,选择器上的'dot'和$ wrapper。试试这个。
I don't have enough reputation to comment, so I will answer here. It seems a typo, the 'dot' on selector and the $ wrapper. Try this.
$('.list_album').each( function () {
var link = $(this).attr('href');
links.push({"link": link});
});
这篇关于用cheerio检索href的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!