'Download a captcha image without an extension

How I can download this captcha image with PIL or another image manipulation library, I tried several ways but I can't download the image.

from PIL import Image
import urllib2 as urllib
import io

fd = urllib.urlopen("https://notacarioca.rio.gov.br/senhaweb/CaptchaImage.aspx?guid=9759fc80-d385-480a-aa6e-8e00ef20be7b&s=1")
image_file = io.BytesIO(fd.read())
im = Image.open(image_file)
print im


Solution 1:[1]

The image you're trying to download doesn't have a static url.

Link working: Link working Same link no longer working: Link not working

This means you can't use a static url for referencing the image (urllib.urlopen("https://notacarioca.rio.gov.br/senhaweb/CaptchaImage.aspx?guid=9759fc80-d385-480a-aa6e-8e00ef20be7b&s=1") won't work).

Here's my solution using Requests and BeautifulSoup:

import requests
from mimetypes import guess_extension
from bs4 import BeautifulSoup
from urllib.parse import urljoin
# from PIL import Image
# from io import BytesIO

s = requests.session()
r = s.get("https://notacarioca.rio.gov.br/senhaweb/login.aspx")

if r.status_code == 200:
    soup = BeautifulSoup(r.content, "html.parser")
    div = soup.find("div", attrs={"class": "captcha", "style": "color:Red;width:100%;"})

    r = s.get(urljoin("https://notacarioca.rio.gov.br/senhaweb/", div.img["src"]))
    if r.status_code == 200:
        guess = guess_extension(r.headers['content-type'])
        if guess:
            with open("captcha" + guess, "wb") as f:
                f.write(r.content)
            # Image.open(BytesIO(r.content)).show()

Solution 2:[2]

Not sure if this needed at this stage since the initial past is several years in the past, although I though I would respond, if someone else needs help with a similar issue. After days of searching I finally got it to work. The Theory is with Selenium 4 you can screenshot an element. This is my code in Powershell:

ForEach ($Img_Element in (Find-SeElement -Driver $browser -TagName img))
    {
    if ($Img_Element.GetAttribute('src') -notlike "*CaptchaImage.aspx*"){Continue}
    $Screenshot = $Img_Element.GetScreenshot().AsBase64EncodedString
    $bytes = [Convert]::FromBase64String($Screenshot)
    [IO.File]::WriteAllBytes($File_CaptchaImg, $bytes)
    Break
    }

Depending on your scripting language, this video may help as well: https://www.youtube.com/watch?v=0pjzLMbtimk (@5:28 if you want to jump to it)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 dǝɥɔS ʇoıןןƎ
Solution 2 hdsouza