美文网首页
nodeJS爬虫(完整版)

nodeJS爬虫(完整版)

作者: bear_new | 来源:发表于2017-11-19 00:04 被阅读0次

nodeJs爬虫

var http = require('http');
var fs = require('fs');
var https = require('https');
var iconv = require('iconv-lite');
var cheerio = require('cheerio');
const request = require('superagent')
require('superagent-charset')(request) // install charset 
var express = require('express');

var url = 'http://price.pcauto.com.cn/shangjia/';
var app = express();

var MongoClient = require('mongodb').MongoClient;
// 数据库cars
var DB_CONN_STR = 'mongodb://localhost:27017/cars'; 
 
http.get(url, function(sres) {
  var html1 = '';
  var chunks = [];

  sres.on('data', function(chunk) {
    chunks.push(chunk);
    html1 += chunk; 
  });

  sres.on('end', function() {
    // 将二进制数据解码成 gb2312 编码数据
    var html = iconv.decode(Buffer.concat(chunks), 'gb2312');
    var $ = cheerio.load(html, {decodeEntities: false});
    writeHtml(html);
    var $dts = $('.dl-brand').find('dt');
    var $dds = $('.dl-brand').find('dd');
    var list = [];


    $dds.each(function(index) {
        var $li = $(this);
        var title = '';
        switch (index) {
            case 0:
                title = '合资品牌';
                break;
            case 1:
                title = '自主品牌';
                break;
            case 2:
                title = '进口品牌';
                break;
        }
        var $a = $li.find('a');
        var brands = [];
        $a.each(function(index) {
            var $one = $(this);
            brands.push({
                link: $one.attr('href'),
                img: $one.find('img').attr('src'),
                name: $one.find('.name').text()
            })
        })

        list.push({
            title: title,
            list: brands
        })
    })

    console.log('列表数据')
    console.log(list)

    MongoClient.connect(DB_CONN_STR, function(err, db) {
        console.log("连接成功!");
        insertData(db, list, function(result) {
            console.log(result);
            db.close();
        });
    });

  });
});

function writeHtml(html) {
    fs.writeFile('index.html', html, function(err) {
        if (err) {
            return console.error(err);
        }
        console.log('写入数据成功!')
    })
}

function insertData (db, data, callback) {
     //连接到表 site
    var collection = db.collection('brand');
    //插入数据
    collection.insert(data, function(err, result) { 
        if(err)
        {
            console.log('Error:'+ err);
            return;
        }     
        callback(result);
    });
}


相关文章

  • nodeJS爬虫(完整版)

    nodeJs爬虫

  • nodejs通过钉钉群机器人推送消息

    nodejs 通过钉钉群机器人推送消息 Intro 最近在用 nodejs 写爬虫,之前的 nodejs 爬虫代码...

  • NodeJs + Phantomjs 简易爬虫

    NodeJs + Phantomjs 简易爬虫 爬虫是什么? 引用百度百科的说法是: 如何在NodeJs上搭建爬虫...

  • Nodejs爬虫

    Node.js批量抓取高清妹子图片:https://cnodejs.org/topic/54bdaac4514ea...

  • NodeJS 爬虫

    技术栈cheerio: 将抓取的html直接转化为jquery对象,可以直接对获取信息进行DOM操作。puppet...

  • nodejs爬虫

    nodejs相关模块 获取网页内容(http\request\superagent等) 筛选网页信息(cheeri...

  • nodejs 爬虫

    爬取的是豆瓣网 本次将会用到两个库:superagent 和cheerio 其中 superagent是用来请求目...

  • nodejs - 爬虫

    继续上一篇写下爬虫的实现,网上找了一个爬虫的文章,然后从里面找了一个网址,https://www.lanvshen...

  • nodejs爬虫

    一、Puppeteer Puppeteer 提供了一系列的 API,可以在无 UI 的情况下调用 Chrome 的...

  • 2018-12-06-项目

    nodejs爬虫:https://github.com/adolt/xmfish-crawler

网友评论

      本文标题:nodeJS爬虫(完整版)

      本文链接:https://www.haomeiwen.com/subject/pltuvxtx.html