代码如下:var http = require("http");var url = "http://www.zhiread.com/";var cheerio = require("cheerio");//获取章节数据function getChpater(html){ var $ = cheerio.load(html); var chapterData = $(".selection-courier-content ul").children("li"); var chapter=[]; chapterData.each(function(item){ var chapter = $(this); var title = chapter.find("h4").text(); var autor = chapter.find("span").text(); var img = chapter.find("img").attr("src"); var chapterItem = { title:title, autor:autor, img:img }; chapter.push(chapterItem); }); return chapter; }// 打印章节function printChpater(chapter){ chapter.forEach(function(item){ console.log("title:"+item.title+" autor:"+item.autor+" img:"+item.img+"/n"); });}http.get(url,function(res){ var html = ""; res.on("data",function(data){ html += data; }); res.on("end",function(){ var chapter = getChpater(html); printChpater(chapter); });}).on("error",function(){ console.log("数据抓取失败~");});一直报错..............
- 3 回答
- 0 关注
- 1138 浏览
添加回答
举报
0/150
提交
取消