Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does jquery crawl page data

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report