var http = require("http");
//Node.js 版的jQuery
var cheerio = require("cheerio");
//写入文件
var fs = require("fs");
var url = "想要爬取网站的网址";
//爬取的通用方法
function httpGet(url, cd) {
var html = "";
http.get(url, function(res) {
res.on("data", function(chunk) {
html += chunk;
});
res.on("end", function(data) {
cd(html);
});
}).on("error", function(e) {
console.log(e.message)
});
}
//调用上面的方法
httpGet(url, function(html) {
var $ = cheerio.load(html);
$(".news-list-a a").each(function(index) {
var newsUrl = $(this).attr("href");
//获取连接后再次调用
httpGet(newsUrl, function(body) {
var jq = cheerio.load(body);
//爬取的内容写入文件
fs.writeFile(`./new/${index}.docx`, jq("#artibody").text(), function(err) {
if(err) {
return console.log(err.message)
}
console.log("爬取完成")
})
})
})
})