尝试抓取网站的首页 ( www.ozbargain.com ) 以返回包含 xbox 引用的 a 标记中的任何内容,但没有任何内容返回到控制台。我认为问题出在带有 :contains 的 if 语句上。
var fs = require('fs'),
request = require('request'),
cheerio = require('cheerio');
url = 'http://www.ozbargain.com.au';
request(url, function(error, response, html) {
if (!error && response.statusCode == 200) {
var $ = cheerio.load(html);
if($("a:contains('Xbox')").length) {
//console.log(this);
var el = $(this);
var log = el.text();
console.log(log);
} else {
console.log('hey');
}
}
});
我正在寻找的 html block 。特别是,我想要 a 标签;
<h2 class="title" id="title214252"><a href="/node/214252">Free on Xbox One, Xbox 360, PS3, PS4: Tales from the Borderlands (Episode 1)</a></h2>
最佳答案
Cheerio 的 contains 语法与 jQuery 略有不同。省略您要搜索的字符串周围的单引号,它应该可以工作:
$("a:contains(Xbox)")
关于javascript - Scraper 不使用 Cheerio 使用 jquery 返回任何值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32980584/