Home  >  Q&A  >  body text

javascript - How to use the request library in nodejs to capture images from web pages

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

滿天的星座滿天的星座2654 days ago811

reply all(1)I'll reply

  • 三叔

    三叔2017-06-17 09:18:39

    /q/10...

    reply
    0
  • Cancelreply