javascript - How to use the request library in nodejs to capture images from web pages
滿天的星座
滿天的星座 2017-06-17 09:16:39
0
1
807
const koa = require('koa');
const request = require('request');
const fs = require('fs');
const path = require('path');
function requestAsync(url) {
    return new Promise((resolve, reject) => {
        request({
            url: url
        }, (err, res, body) => {
            if (err) {
                reject(err);
            } else {
                resolve(body);
            }
        })
    });
}
function writeFileAsync(path, data, option) {
    return new Promise((resolve, reject) => {
        fs.writeFile(path, data, option, (err)=> {
            if (err) {
                reject(err);
            } else {
                resolve();
            }
        })
    });
}
const app = new koa();
app.use(async (ctx) => {
    let url = 'http://pubimage.360doc.com/index7/bannerl_1.jpg';
    let filepath = path.join(__dirname, './images/详情');
    filepath = filepath + '/1.jpg';
    // request(url).pipe(fs.createWriteStream(filepath));这个可以正常抓取图片
    let data = await requestAsync(url);
    let buffer = Buffer.from(data);
    await writeFileAsync(filepath, buffer);
    ctx.type = 'jpg';
    ctx.body = buffer;
})
app.listen(3000);
console.log(`starting at ${3000}`);

I wrote both request and writefile in the form of promise. It is possible to capture html, but not images.

At the beginning, the data output is of string type, that is, the body of the request is of string type, so I use Buffer.from

Converting to buffer type does not work, nor does changing it to Buffer.from(data, 'base64'), but using request(url).pipe(fs.createWriteStream(filepath)) will do.
Excuse me Where is the mistake

滿天的星座
滿天的星座

reply all(1)
三叔

/q/10...

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!