A probe into node. JS Crawlers

Source: Internet
Author: User

Objective

In the Learning Network video and Cnode Novice access to the crawler, said is the crawler, in fact, did not use crawler-related third-party class library, mainly using the node. JS Basic module HTTP, Web analysis tools Cherrio. Use HTTP to get the URL path directly corresponding to the Web resource, and then use Cherrio analysis. Here I mainly put the network teaching video to provide the case of their own knocked on one side, deepen understanding. In the process of coding, the first time I took the JQ object directly with the foreach traversal, direct error, because JQ does not correspond to this method, only the JS array can be called.

Knowledge points

①:superagent grab the Web tool. I haven't used it for the time being.

②:cherrio Web Analytics tool, you can understand it as a service-side jquery, because the syntax is the same.

1. Crawl the entire page

2, the analysis of the data, I here is the MU-class network provides examples of examples for the implementation of the case.

Research on source code analysis of Reptiles
var http=require (' http '); var cheerio=require (' Cheerio '); var url= ' http://www.imooc.com/learn/348 ';/*************** Printed data structure [{chaptertitle: ', Videos:[{title: ', ID: '}]}]********************************/function Printcourseinfo (coursedata) {Coursedata.foreach (function (item) {var chaptertitle=item.chaptertitle;console.log ( Chaptertitle+ ' \ n '), Item.videos.forEach (function (video) {Console.log (' "' +video.id+ '" ' +video.title+ ' \ n ');})});} /************* analyze the data crawled from the Web page **************/function filterchapter (HTML) {var coursedata=[];var $=cheerio.load (HTML); var chapters=$ ('. Chapter '); Chapters.each (function (item) {var chapter=$ (this); var chaptertitle=chapter.find (' Strong '). text (); Find Chapter title var videos=chapter.find ('. Video '). Children (' Li '); var chapterdata={chaptertitle:chaptertitle,videos:[]}; Videos.each (function (item) {var video=$ (this). Find ('. Studyvideo '); var title=video.text (); var id=video.attr (' href ') . Split ('/video ') [1];chapterdata.videos.push ({title:title,id:id})}) Coursedata.push (Chapterdata);});    return coursedata;} Http.get (Url,function (res) {var html= '; Res.on (' Data ', function (data) {html+=data;}) Res.on (' End ', function () {var coursedata=filterchapter (HTML);p rintcourseinfo (coursedata);})}). On (' Error ', function () {Console.log (' Error getting course data ');})

Resources

Https://github.com/alsotang/node-lessons/tree/master/lesson3

http://www.imooc.com/video/7965

A probe into node. JS Crawlers

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.