nodejs--implementing cross-domain Fetch data

Source: Internet
Author: User

Recently the company arranged to give me a task to crawl the page data; http://survey.finance.sina.com.cn/static/20205/20131120.html?pid=20205&dpc=1, as a front end, Have not done backstage things, bite the bullet after, on the internet to find a variety of ways. Finally found a Nodejs method, after practice, feel very useful, so share to everyone. This tutorial starts from 0 and is suitable for children's shoes that have never been exposed to Nodejs!
1. Download Nodejs First, this does not say much, and then double-click Install File installation. : http://www.nodejs.org/download/;
2. After the installation is complete, open cmd directly, type Node-v, output node version number, and then enter NPM-V, output NPM version number, indicating the successful installation, continue to the next step;
3. Type NPM install express-g, meaning to install the Express server to the global, the role is to create a project later;
4. Type the NPM install JADE-G, installing the Jade module;
5. Type the NPM install cheerio-g, install the Cheerio module, to parse the HTML is very convenient, just like using jquery in the browser, it can be understood as a set of rules of the module.
6. Continue typing express-v (here is uppercase, because the new version is recognized), if the success will prompt the version number of Express, if failed, prompt "Express is not internal command", you need to type the command NPM install Express-generator-g;
7. Enter Express-v again, it will appear normal, in addition, enter the node list to see which modules you have installed. ;
8. Then enter the express MyApp; the function is to create its own project, called MyApp, the path is c/user/xxx/myapp; it is not possible to search for MyApp on the computer.
9. Then you can add JS file in the project, create a new Load.js file, and create a new Number.txt file in the directory, the code will be used;
10. The contents are:

varHTTP = require ("http");//Utility function that downloads a URL and invokes//callback with the data.functionDownload (URL, callback) {http.get (URL,function(res) {vardata = ""; Res.on (' Data ',function(chunk) {data+=Chunk;    }); Res.on ("End",function() {callback (data);  }); }). On ("Error",function() {Callback (NULL); });}varCheerio = require ("Cheerio");varFS =require (' FS '); varurl = "Http://survey.finance.sina.com.cn/static/20205/20131120.html?pid=20205&dpc=1"; Download (URL,function(data) {if(data) {//console.log (data);          var$ =cheerio.load (data); varTotal =$ (". Fred")). text (); Fs.appendfile ('./number.txt ', total, ' utf-8 ',function(err) {if(ERR) {Throwerr;}          }); varnumarrs=$ ("div[class= ' d d-num ']"). text (); Numarrs=numarrs.replace (/,/g, ");//turn all the commas into empty (the comma in the middle of the number)Numarrs =numarrs.replace (/\s+/g, ', ');//Turn all empty strings into a comma (a comma between numbers and numbers)Numarrs =numarrs.replace (/,$/gi, ");//remove the last commaFs.appendfile ('./number.txt ', Numarrs, ' Utf-8 ',function(err) {if(ERR) {Throwerr;}          }); Console.log ("Done"); }        ElseConsole.log ("Error"); });

11. Then enter CMD, enter the CD MyApp, enter the MyApp directory, then enter the command node Load.js, execute the JS file, then you can open the Number.txt file to see that there is the data you want, as shown in.

Execution effect:

File effects:
Here, the simple crawl data is over. Of course, can also be extended, such as Write loop, date transformation, you can get more different pages of data.

nodejs--implementing cross-domain Fetch data

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.