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

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

内容简介:之前写过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实现超简单爬虫

原文链接:


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

查看所有标签

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

High-Performance Compilers for Parallel Computing

High-Performance Compilers for Parallel Computing

Michael Wolfe / Addison-Wesley / 1995-6-16 / USD 117.40

By the author of the classic 1989 monograph, Optimizing Supercompilers for Supercomputers, this book covers the knowledge and skills necessary to build a competitive, advanced compiler for parallel or......一起来看看 《High-Performance Compilers for Parallel Computing》 这本书的介绍吧!

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

RGB HEX 互转工具

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具