使用 node.js 通过 cheerio 通过网页抓取获取 IMG SRC

Get IMG SRC via web scraping through cheerio with node.js

使用事件侦听器获取 this 并使用 cheerio 从以下位置获取 img src:

<div class="mainimage"> 

当前代码:

var cheerio = require('cheerio'),
$ = cheerio.load(this.responseText);
console.log($('mainimage').attr('img'));

然而 returns 'undefined'

首先你需要有 .mainimage(注意圆点),因为这就是 jQuery 用于选择具有特定 class 的内容的语法。然后你想访问 .mainimage div 内的 <img> 标签。最后我们可以从图像标签中读取 src 属性:

console.log($('.mainimage img').attr('src'));