How does jquery crawl page data
This article mainly explains "how jquery crawls the page data". Friends who are interested may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how jquery crawls the page data.
Build the environment:
(1) create a folder, enter and initialize a package.json file.
Npm init-y
(2) installation related dependencies:
Npm install-- save koa? Npm install-- save cheerio / / will be used later for crawling the page module, a fast, flexible and implemented jQuery core implementation specially customized for the server.
Now let's have a simple demo warm-up. Add the code directly (file name: demo2.js):
Var http=require ('http') / / Node.js provides a http module for building HTTP servers and clients
Var url='' / / enter any URL is fine
Http.get (url,function (res) {/ / send get request
Var html=''
Res.on ('data',function (data) {
Html + = data / / string concatenation
})
Res.on ('end',function () {
Console.log (html)
})
}). On ('error',function () {
Console.log ('error getting resource!' )
})
Execute node demo2.js? The results are as follows:
It's amazing. However, this is not what we want, what we want is to get some part of the data on the page.
Here, take the TV drama page of no cinema as an example. At this time, cheerio will come in handy. It has been installed before, so I won't repeat it here. Its usage is actually very similar to jquery. Reference? Cheerio (explanation of Baidu Encyclopedia)
Introduce the cheerio module into the js file just now, and then load the required html content.
Var $= cheerio.load (html)? / / load the required html
For convenience, a function is encapsulated here:
FunctionfilterChapters (html) {var $= cheerio.load (html) / / load the required html, and then you can happily use a syntax like jQuery
Var chapters=$ ('.movie-item') / / find the class of the required resources in html
Var courseData= [] / / create an array to hold resources
Chapters.each (function (item, index) {/ / traverses our html documents
At this point, I believe you have a deeper understanding of "how jquery crawls the page data". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!