Git Product home page Git Product logo

node's Introduction

关于node几个简单的例子 1)第一个如何爬界面的图片 2)第二个如何爬界面的数据保存到本地,并用node的fs对文件进行操作。 主要操作 npm install require npm install cheerio var request = require('request'); var fs = require('fs'); var path = require('path'); var cheerio = require('cheerio'); var url = "http://dig.chouti.com/"; request(url,function(error,response,body){ if(!error&&response.statusCode ==200){ //console.log(body); getData(body); }else{ console.log("抓取数据失败!"); } }); function getData(data){ var $ = cheerio.load(data); var arr = []; $('#content-list .part2').each(function(index,element){ var $this = $(element); var data = { title: $this.attr('share-title'), href: $this.attr('href'), img: $this.attr('share-pic')

	};
	arr.push(data);
})
saveData(arr);

} function saveData(data){ var temp = JSON.stringify(data); fs.writeFile("bb.json",temp,function (err) { if (err) throw err ; console.log("File Saved !"); //文件被保存 }); fs.exists("bb.json",function(err){ console.log(err) }); fs.stat('bb.json', function(err, stat){ console.log(stat); }); fs.rename('bb.json',"test.json",function(err){ if(!err)console.log("rename success!"); }) }

node's People

Contributors

j512581813 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.