Ajax + node + request crawling network images (otaku benefits ),

Source: Internet
Author: User

Ajax + node + request crawling network images (otaku benefits ),

Note: This article only discusses technology that does not involve commerce. If there is any infringement, please let us know and repost it without your consent!

This article crawls some image resources of "youguo net" Through ajax on the browser and node request-json, which is purely technical and does not involve commercial aspects;

First:

 

If you do not have the node basics, learn it yourself ~

How to obtain images: request an html file and obtain an array of images on the current page using the regular expression matching image path. Send the array to the browser for display;

1. Install request-json (cnpm I request-json -- save)

2. Install express (cnpm I express -- save)

3. Create an app. js file as a server file. The Code is as follows:

Const express = require ("express ");
Const morgan = require ('Morgan ');
Const ejs = require ('ejs ');
Const path = require ('path ');
Const bodyParser = require ('body-parser ');
Const app = express ();

// Logs info to server
App. use (morgan ('dev '));

// Post resolve
App. use (bodyParser. json ());
App. use (bodyParser. urlencoded ({
Extended: false
}));

// View engine setup
App. engine ('html ', ejs. _ express );
App. set ('view', path. join (_ dirname, 'view '));
App. set ('view engine ', 'html ');

// Set static files such as images, CSS, and JavaScript.
App. use (bodyParser. json ());
App. use (bodyParser. urlencoded ({
Extended: false
}));
App. use (express. static (path. join (_ dirname, 'public ')));

/*
* Reuire pages
*/
Var index = require ('./routes/Index ')

/*
* Render pages
*/
App. use ('/', index );

// Catch 404 and forward to error handler
App. use (function (req, res, next ){
Var err = new Error ('not Found ');
Err. status = 404;
Next (err );
});
// Error handler
App. use (function (err, req, res, next ){
// Set locals, only providing error in development
Res. locals. message = err. message;
Res. locals. error = req. app. get ('env') = 'development '? Err :{};

// Render the error page
Res. status (err. status | 500 );
Res. render ('error ',{
"Title": '000000 ',
"Msg": 'service exception'
});
});
Module. exports = app;

App. listen (3000, function (){
Console. log ('HTTP: // 127.0.0.1: 3000 ')
});

The service runs on port 3000;

4. Request html page:

Router. all ("/getUGirls", function (req, res, next ){

Regular part of the code (......)
Client. get (url, function (err, response, body ){
If (typeof body )! = "String "){
Body = JSON. stringify (body );
}
Arr = body. match (reg );
Console. log (arr );

// Here is the path of the current page and the array of the page part list, which is sent to the client through res. json;
Res. json ({"url": url, "records": arr });
});
})

This method applies to crawling images of any website with rules on the page url and the image path in the page;

Again, I declare that you should not use images on other websites as you like. Learning Technology is good. Besides, this method has no technical content and the source code will not be available;

If you don't talk about it, you can see the picture.

Related Article

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.