[英]Scraper not returning any values with jquery using cheerio
Trying to scrape the front page of a website ( www.ozbargain.com ) to return any content in the a tag that holds a reference to xbox but nothing is being returned to console. 尝试抓取网站的首页( www.ozbargain.com ),以返回标记中包含对xbox的引用的标记中的任何内容,但没有任何内容返回到控制台。 I believe the issue is with the if statement with :contains.
我相信问题在于带有: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');
}
}
});
The html block I'm after. 我追随的html块。 In particulare, I want the a tag;
特别是,我想要一个标签。
<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>
The Cheerio syntax for contains is slightly different than jQuery. 包含的Cheerio语法与jQuery略有不同。 Ommit the single quotes around the string you're searching for an it should work:
省略您要搜索的字符串周围的单引号,它应该可以工作:
$("a:contains(Xbox)")
Assigned the selector to a variable then called the text method. 将选择器分配给一个变量,然后称为文本方法。
request(url, function(error, response, html) {
if (!error && response.statusCode == 200) {
var $ = cheerio.load(html);
var $el = $("a:contains('Xbox')");
if ($el.length) {
console.log($el.text());
} else {
console.log('hey');
}
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.