美文网首页
nodeJS数据抓取

nodeJS数据抓取

作者: 9ac64e1f7a99 | 来源:发表于2017-05-08 04:07 被阅读1066次

工具

项目目录结构

项目目录结构
package.json
babel

watcher.js

const chokidar = require('chokidar');
const shell = require('shelljs');


const watcher = chokidar.watch('.', {
  ignored: [
    /[\/\\]\./,
    /node_modules/,
    /vscode/,
    /babelrc/,
    /watcher.js/,
    /package.json/
    ], persistent: true
});

const log = console.log.bind(console);
const modify = () => {
  shell.exec('clear && npm start');
}

watcher
  .on('add', function(path) { log('File', path, 'has been added'); }) 
  .on('addDir', function(path) {  log('Directory', path, 'has been added'); })
  .on('change', function(path) { modify();})
  .on('unlink', function(path) {  log('File', path, 'has been removed'); })
  .on('unlinkDir', function(path) {  log('Directory', path, 'has been removed'); })
  .on('error', function(error) { log('Error happened', error); })
  .on('ready', function() {   log('Initial scan complete. Ready for changes.'); }); 
  // .on('raw', function(event, path, details) { log('Raw event info:', event, path, details); }) 


modify();

getPage.js

const http = require("http");

// Utility function that downloads a URL and invokes
// callback with the data.
function download(url, callback) {
  http.get(url, function(res) {
    let data = "";
    res.on('data', function (chunk) {
      data += chunk;
    });
    res.on("end", function() {
      callback(data);
    });
  }).on("error", function() {
    callback(null);
  });
}

export default download;

index.js 数据抓取

// 抓取虾米主页的新碟首发
const cheerio = require("cheerio");
const fs = require('fs');
const path = require('path');
import getPage from './util/getPage';

const URL = 'http://www.xiami.com/';

getPage(URL, (data) => {
  const jsonObj = [];
  if(data) {
    const $ = cheerio.load(data);
    $('#albums').find('.content_block').children(function(i, e){
      const $image = $(e).find('.image');
      const $info = $image.next();
      jsonObj.push({
        img: $image.children('img').attr('src'),
        url: URL + $image.children('a').attr('href'),
        name: $info.find('a').text()
      });
    });
  }
  // 将抓取的数据写入的文件中去
  fs.writeFile(path.resolve(__dirname, 'test.json'), JSON.stringify(jsonObj), (err) => {
    console.log(err);
  });
});

相关文章

  • nodeJS数据抓取

    工具 chokidar 文件监听 shelljs 执行命令行 cheerio html页面解析 非常nice的解析...

  • NodeJS 用jsdom抓取html数据

    先要装jsdom: npm install jsdom. var jsdom = require('jsdom')...

  • nodejs request CURl 抓取网页数据

    Node 利用request获取API、网页数据 本例用于通过微信授权回调code获取UserInfo信息

  • 04_中央气象台

    简述 再次进行分析抓取气象数据练习,本节主要抓取预报气象数据。抓取数据请勿存档,商用请联系官方。 爬取对象 抓取中...

  • iOS 防止 Charles 抓取数据

    iOS 防止 Charles 抓取数据 iOS 防止 Charles 抓取数据

  • 03_中央气象台

    简述 继续分析抓取气象数据练习,本节主要抓取实时气象数据。抓取数据请勿存档,商用请联系官方。 爬取对象 抓取中央气...

  • requests-code说明

    Charles抓取的request的raw数据 chrome抓取的network数据

  • 服务端nodejs抓取jsonp接口数据

    众所周知,jsonp 接口返回的是一段 js 脚本,在浏览器中使用 script 标签引入、加载成功后,会直接执行...

  • 人人都会数据分析大纲

    -实现数据分析需要有哪些东西? -有数据 --数据从何而来? ---自有数据 ---爬虫抓取 ----爬虫抓取的步...

  • NodeJs的一次实用(定时抓取数据)

    起因 想定时抓取某个接口的数据,然后解析数据,通过邮件通知给我。 用到的一些库 1、https 用来发送网络请求2...

网友评论

      本文标题:nodeJS数据抓取

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