nodejs做的爬虫,爬取腾讯动漫的内容

首先上package.json

{
  "name": "Spider",
  "version": "1.0.0",
  "description": "spider ",
  "main": "index.js",
  "dependencies": {
    "async": "^1.5.0",
    "cheerio": "^0.19.0",
    "eventproxy": "^0.3.4",
    "superagent": "^1.4.0"
  },
  "devDependencies": {},
  "scripts": {
    "test": "node index",
    "start": "node server.js"
  }
}

再上server.js

var http = require("http");
var cheerio = require("cheerio");
var fs = require('fs');
//Utility function that downloads a URL and invokes
//callback with the data.
function downloadPage(url, callback) {
    http.get(url, function(res) {
        var data = "";
        res.on('data', function(chunk) {
            data += chunk;
        });
        res.on("end", function() {
            callback(data);
        });
    }).on("error", function() {
        callback(null);
    });
}


function start() {
    var url = 'http://ac.qq.com/Comic/index/type/4/page/';
    var url2 = 'http://ac.qq.com/ComicView/index/id/549690/cid/1';
    var arr = [];
    for (var i = 1; i < 13; i++) {
        downloadPage(url + i, function(data) {
            if (data) {
                var $ = cheerio.load(data);
                $("div.ret-search-result > ul > li.ret-search-item").each(function(i, e) {
                    var json = {};
                    json.tags = [];
                    json.img = $(e).find('img').attr('data-original');
                    json.link = $(e).find('a.mod-cover-list-thumb').attr('href');
                    json.id = json.link.split('/').reverse()[0];
                    json.title = $(e).find('h3.ret-works-title > a').text();
                    json.author = $(e).find('p.ret-works-author').text();
                    json.popular = $(e).find('p.ret-works-tags> span > em').text();
                    json.description = $(e).find('p.ret-works-decs').text();
                    $(e).find('p.ret-works-tags>a').each(function(i, e) {
                        json.tags.push($(e).text());
                    });
                    downloadImg(json.img);
                    arr.push(json)
                    console.log("done");
                    // console.log(arr)
                    // fs.writeFileSync('./output.json', JSON.stringify(arr));
                    // });
                })
            }
        })
    }
}

function downloadImg(url) {
    console.log('string')
    http.get(url, function(res) {
        var imgData = "";
        res.setEncoding("binary"); //一定要设置response的编码为binary不然会下载下来的图片打不开
        res.on("data", function(chunk) {
            imgData += chunk;
        });
        res.on("end", function() {
            var d = new Date();
            fs.writeFile("./downImgs/" + Math.floor(Math.random() * 10000000) + '.jpg', imgData, "binary", function(err) {
                if (err) {
                    console.log(err);
                }
                console.log("down success");
            });
        });
    });
}
exports.start = start;

末了是index.js

var server = require("./server");

server.start();

申明

1,引入必需的模块,http, cheerio, fs

2,downloadPage函数吸收URL,并在回调里处置惩罚数据。

3,start函数里,定义url数据源,这里用的是腾讯动漫.

4,for轮回处置惩罚url数据内容,内里的downloadImg函数,即保留图片到当地。

    原文作者:紫气东来_姜波
    原文地址: https://segmentfault.com/a/1190000008557412
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞