E
E
Eugene2019-01-24 22:49:39
Node.js
Eugene, 2019-01-24 22:49:39

How to download captcha image?

Using the http or request module, you need to download the captcha image.

const  request = require('request');
const uri = 'https://ext.captcha.yandex.net/image?key=001H7F4Pt8xuKG6qu8iZQHJxXHaANjOE';
request.head(uri, function(err, res, body){
        console.log('content-type:', res.headers['content-type']);
        console.log('content-length:', res.headers['content-length']);
        request(uri).pipe(fs.createWriteStream('cap.gif'));
    });


content-type: undefined
content-length: undefined

const https = require('https');
const fs = require('fs');

https.get('https://ext.captcha.yandex.net/image?key=001H7F4Pt8xuKG6qu8iZQHJxXHaANjOE', function(response){
    response.pipe(fs.createWriteStream('captcha.gif'));
});

Both examples save an image with the following content.
5c4a16acc497a503069200.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
Grigory S., 2019-01-26
@you_are_enot

const fs = require('fs');
const { resolve } = require('path');
const { promisify } = require('util');
const writeFile = promisify(fs.writeFile);
const got = require('got');

async function getImage(url)
{
  try
  {
    const { body, statusCode, statusMessage } = await got(url, { encoding: null });
    
    if (statusCode === 200)
    {
      await writeFile(resolve('.', 'newCaptchaAnchor.gif'), body);
    }
    else
    {
      throw new Error(statusMessage);
    }
  }
  catch(err)
  {
      throw new Error(err);
  }
}

getImage('https://developers.google.com/recaptcha/images/newCaptchaAnchor.gif')
  .then(() => console.log('Complete....'));

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question