Nodejs爬虫,使用cheerio+request+phantomjs实现超简单爬虫

栏目: Node.js · 发布时间: 6年前

内容简介:之前写过golang里比较好用的爬虫工具是 goquery[传送门]今天来介绍一下nodejs里的爬虫使用npm初始化一个nodejs项目

之前写过golang里比较好用的爬虫 工具 是 goquery[传送门]

今天来介绍一下nodejs里的爬虫

创建项目

使用npm初始化一个nodejs项目

# 创建一个文件夹 crawling
mkdir crawling
# 进入文件夹并初始化
cd crawling
npm init

安装依赖

yarn add cheerio request iconv-lite
  • cheerio 像jquery一样用来解析网页的
  • request http请求的工具
  • iconv-lite request默认编码是utf-8, 如果网页不是utf-8编码,可以用来转码

封装一下request

var request = require('request');
var iconv = require('iconv-lite');

module.exports = function(url, method, encoding, callback) {
  request({
    url: url,
    method: method,
    encoding: null,
    // proxy: 'http://127.0.0.1:1087',
    headers: {
      'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36'
    }
  }, function(err, res, body) {
    body = iconv.decode(body, encoding);
    if (err) {
      console.log(err);
    } else {
      callback(body);
    }
  })
}

开始爬取

var request = require('./request');
var cheerio = require('./cheerio');

function fetch() {
  request('https://cnodejs.org/', 'get', 'utf-8', function(body) {
    var $ = cheerio.load(body);
    $('#topic_list').find('.cell').each(function(i, v) {
      var title = $(v).find('.topic_title').text();
      var href = 'https://cnodejs.org' + $(v).find('.topic_title').attr('href');
      console.log(title, href);
    })
  })
}

运行结果

Nodejs爬虫,使用cheerio+request+phantomjs实现超简单爬虫

抓取网站是js渲染的

现在前端这么流行,很多网站都是用js框架写的了,这导致页面都是用js渲染的,普通的http请求拿到的只是html页面,它不会执行js,所以也就没有内容了,下面介绍一下用phantomjs来抓取js渲染的网页内容

这里用网易新闻手机版的,打开链接 https://3g.163.com/touch/news/ 然后查看页面源代码,可以看到body里是没有内容的

Nodejs爬虫,使用cheerio+request+phantomjs实现超简单爬虫

安装依赖

yarn add phantom
var phantom = require('phantom');

function news() {
  var sitepage, phInstance;
  phantom.create()
    .then(function (instance) {
      phInstance = instance;
      return instance.createPage();
    }).then(function (page) {
      sitepage = page;
      return page.open('https://3g.163.com/touch/news/');
    }).then(function (status) {
      return sitepage.property('content');
    }).then(function (content) {
      var $ = cheerio.load(content);
      $(".recommend-list>article").each(function (i, v) {
        var title = $(v).find('.title').text();
        var href = $(v).find('a').attr('href');
        console.log(title, href);
      });
    }).then(function() {
      sitepage.close();
      phInstance.exit();
    }).catch(function (err) {
      phInstance.exit();
    })
}

运行结果

Nodejs爬虫,使用cheerio+request+phantomjs实现超简单爬虫

原文链接:


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

MySQL权威指南

MySQL权威指南

Randy Jay Yarger / 林琪、朱涛江 / 中国电力出版社 / 2003-11-1 / 49.00元

为一种开源数据库,MySQL已经成为最流行的服务器软件包之一。开发人员在其数据库引擎中提供了丰富的特性(只需很少的内存和CPU支持)。 因此,众多Linux和Unix服务器(以及一些Windows服务器)都采用MySQL作为其数据库引擎。由于MySQL作为Web站点后端时速度特别快而且相当方便,所有在目前流行的一个词LAMP(表示Linux、Apache、MySQL和Perl、Python或......一起来看看 《MySQL权威指南》 这本书的介绍吧!

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

SHA 加密
SHA 加密

SHA 加密工具