nodejs python爬虫_nodejs模块 – phantomjs的简单爬虫应用实例

nodejs模块 – phantomjs的简单爬虫应用实例

PhantomJS是一个可以用JavaScript编写脚本的无头web浏览器。它可以在Windows、macOS、Linux和FreeBSD上运行。

它使用QtWebKit作为后端,为各种web标准(DOM处理、CSS选择器、JSON、Canvas和SVG)提供快速和本机支持。

安装phantomjs

npm i phantom --save-dev

0

npmiphantom--save-dev

安装Cheerio, Cheerio是一个服务器端基本实现Jquery核心功能的一个库

npm i cheerio --save-dev

0

npmicheerio--save-dev

实例一(这是官方首页给的一个实例):

下面这个用于PhantomJS的简单脚本加载百度主页,稍等片刻,然后将其捕获到一个图像中。

var page = require('webpage').create();

page.open('http://www.baidu.com', function() {

setTimeout(function() {

page.render('google.png');

phantom.exit();

}, 200);

});

0

1

2

3

4

5

6

varpage=require('webpage').create();

page.open('http://www.baidu.com',function(){

setTimeout(function(){

page.render('google.png');

phantom.exit();

},200);

});

实例二:

使用WebStorm创建一个NodeJS-Express项目,项目创建好以后,修改项目的index.js文件。

目的:获取百度首页上所有能匹配 http[s]?://.* 的a标签,因此有:

const express = require('express');

const router = express.Router();

const phantom = require('phantom');

const cheerio = require('cheerio');

/* GET home page. */

router.get('/', function (req, res, next) {

res.header('Content-Type', 'application/json');

let sitepage = null; //创建网页对象实例

let phInstance = null; //创建phantomj实例对象

phantom.create()

.then(instance => {

phInstance = instance;

return instance.createPage();

})

.then(page => {

sitepage = page;

return page.open('https://www.baidu.com/');

})

.then(status => {

console.info(status); //获取结果状态

return sitepage.property('content'); //获取相应的属性内容

})

.then(content => {

const $ = cheerio.load(content); //解析输出的结果内容

const jsonResult = [];

$('a[href]').each((i, item) => { //抓取符合条件的a标签的链接地址

const href = $(item).attr('href');

if (new RegExp(/http[s]?:\/\/.*/).test(href)) {

jsonResult.push(href);

}

});

sitepage.close();

phInstance.exit();

res.json(jsonResult);

})

.catch(error => {

console.log(error);

phInstance.exit();

res.json({status: false});

});

});

module.exports = router;

0

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

constexpress=require('express');

constrouter=express.Router();

constphantom=require('phantom');

constcheerio=require('cheerio');

/* GET home page. */

router.get('/',function(req,res,next){

res.header('Content-Type','application/json');

letsitepage=null;//创建网页对象实例

letphInstance=null;//创建phantomj实例对象

phantom.create()

.then(instance=>{

phInstance=instance;

returninstance.createPage();

})

.then(page=>{

sitepage=page;

returnpage.open('https://www.baidu.com/');

})

.then(status=>{

console.info(status);//获取结果状态

returnsitepage.property('content');//获取相应的属性内容

})

.then(content=>{

const$=cheerio.load(content);//解析输出的结果内容

constjsonResult=[];

$('a[href]').each((i,item)=>{//抓取符合条件的a标签的链接地址

consthref=$(item).attr('href');

if(newRegExp(/http[s]?:\/\/.*/).test(href)){

jsonResult.push(href);

}

});

sitepage.close();

phInstance.exit();

res.json(jsonResult);

})

.catch(error=>{

console.log(error);

phInstance.exit();

res.json({status:false});

});

});

module.exports=router;

运行以上代码后,页面中输出链接集合。

——————————

使用标准的DOM API或jQuery等常用库访问web页面并提取信息,玩爬虫的,就好好研究这个了。

以编程方式捕获web内容,包括SVG和Canvas,创建网站截图与缩略图预览。

使用诸如Jasmine、QUnit、Mocha、WebDriver等框架运行功能测试。

监控页面加载和导出为标准的HAR文件。使用YSlow和Jenkins自动化性能分析。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值