Web Spider

作者: 方寸拾光 | 来源:发表于2018-06-04 15:00 被阅读18次
Web Spider

最近女票公司又要苦逼的统计一些数据了,然而我这次放弃了直接从网站上一个一个的数据复制粘贴到Excel。因为这是一项劳民伤财的工程,还好这次给了我足够的时间来研究。下面是我做的爬虫代码。

代码中用到的主要工具有两个,一个是Cheerio,是node端的jquery(能做一些类似前端的选择器的操作,几乎和前端的一样)。另外一个神器是Node-xlsx,是把爬取的数据放入excel的工具,使用也是相当简单。

主文件就是这个 tencent-spider.js

const

    http = require('http'),

    fs = require('fs'),

    cheerio = require('cheerio'),

    xlsx = require('node-xlsx');

const writeXlsx = datas => {

let buffer = xlsx.build([

{

name:'Tencent Video Reading',

            data: datas

}

]);

    fs.writeFileSync('./harvest/tencent/1.xlsx', buffer, {'flag':'w'});  //生成excel

};

//该函数的作用:在本地存储所爬取的新闻内容资源

const savedContent = $ => {

let dataArr = [];

    dataArr.push(['标题', '阅读量', '时间']);

    $('.figures_list li').each(function (index, item) {

let title = $(this).find('strong a').text(),

            reading = $(this).find('.figure_info .info_inner').text(),

            time = $(this).find('.figure_info .figure_info_time').text();

        console.log(title);

        let data = [title, reading, time];

        dataArr.push(data);    //一行一行添加的 不是一列一列

    });

    writeXlsx(dataArr);

};

const startRequest = x => {

//采用http模块向服务器发起一次get请求

    http.get(x, function (res) {

let html ='';        //用来存储请求网页的整个html内容

        res.setEncoding('utf-8'); //防止中文乱码

        //监听data事件,每次取一块数据

        res.on('data', function (chunk) {

html += chunk;

        });

        //监听end事件,如果整个网页内容的html都获取完毕,就执行回调函数

        res.on('end', function () {

let $ =cheerio.load(html); //采用cheerio模块解析html

            savedContent($);  //存储每篇文章的内容及文章标题

        });

    }).on('error', function (err) {

console.log(err);

    });

};

startRequest("http://v.qq.com/vplus/wevideo/videos");      //主程序开始运行

## Explore Spider :watermelon:

In this repo, I will crawl different website data.

## Tech Stack :strawberry:

- [Node](https://nodejs.org)

- [Cheerio](https://cheerio.js.org)

- [Node-xlsx](https://github.com/mgcrea/node-xlsx)

## Installation :green_apple:

```

$ git clone https://github.com/JimmieMax/explore-spider.git

$ npm install

```

## Run tasks :banana:

```

//tencent spider http://v.qq.com/vplus/wevideo/videos

node tencent-spider

```

## Authors :cherries:

Jimmie Max

相关文章

  • Web Spider

    最近女票公司又要苦逼的统计一些数据了,然而我这次放弃了直接从网站上一个一个的数据复制粘贴到Excel。因为这是一项...

  • python爬虫初体验

    网络爬虫(Web Spider)是什么 网络爬虫也叫网络蜘蛛,即Web Spider,名字也是非常之形象。 如果把...

  • The Internet

    The internet is like an enormous spider web, confines peo...

  • 初识Spider

    Spider 网络爬虫(web crawler),以前经常称之为网络蜘蛛(spider),是按照一定的规则自动浏览...

  • grep 日志分析

    grep -c -i 'Sogou web spider' /home/wwwlogs/2015/04/acces...

  • 2.3、User’s guide (Queue)

    Queue example a concurrent web spider 上一篇: 2.2、User’s gui...

  • Python Scrapy 实战

    Python Scrapy 什么是爬虫? 网络爬虫(英语:web crawler),也叫网络蜘蛛(spider),...

  • 什么是网络爬虫?

    什么是Web Spider 网络爬虫? 如果互联网比喻成一个蜘蛛网,那么Spider就是在网上爬来爬去的蜘蛛,网络...

  • 网鼎杯第一场wp(web)

    web1~~~~ spider 访问 robots.txt 发现存在 /get_sourcecode 文件,访问该...

  • 工具使用 - 草稿

    wvs10.5 web扫描 burp suite 代理 修改数据包 spider(文件目录扫描)repeater(...

网友评论

    本文标题:Web Spider

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