Node.js 抓取網頁中的價格


通過 JSDOM

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#!/usr/bin/env node

var jsdom = require('jsdom'),
colors = require('colors'),
printf = require('printf'),
dateFormat = require('dateformat');

jsdom.env({
url: "http://www.example.com/goods/show/99",
scripts: ["http://code.jquery.com/jquery.js"],
done: function (errors, window) {
var $ = window.$;

var name = $("table.jmb tr:eq(2) td span:first").text().trim();
var price = $("table.jmb tr:eq(2) td span:last").text().trim().match(/(\d+).*=.*\s(\d+).*=.*/);
var price1b = price[1];
var price10b = price[2];

console.log(printf(
"%-30s %-30s %-30s %-20s %-30s",
"Example".yellow, price1b.green, price10b.green,
+Date.now(), dateFormat(Date.now(), "yyyy-mm-dd HH:mm::ss")
));
}
});

通過 cheerio 和 request

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#!/usr/bin/env node

var cheerio = require('cheerio'),
request = require('request'),
colors = require('colors'),
printf = require('printf'),
dateFormat = require('dateformat');

request("http://www.example.com/goods/show/99", function(error, response, html) {
if (error) {
return console.error(error);
}

var $ = cheerio.load(html);

var name = $("table.jmb tr").eq(2).find("td span").first().text().trim();
var price = $("table.jmb tr").eq(2).find("td span").last().text().trim().match(/(\d+).*=.*\s(\d+).*=.*/);
var price1b = price[1];
var price10b = price[2];

console.log(printf(
"%-30s %-30s %-30s %-20s %-30s",
"Example", price1b, price10b,
+Date.now(), dateFormat(Date.now(), "yyyy-mm-dd HH:mm::ss")
));
});