如何使用PIL或其他图像操作库下载this验证码图像,我尝试了几种方法,但无法下载图像。
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发布于 2017-11-01 18:27:55
您尝试下载的图像没有静态url。
链路工作正常:

同样的链接不再起作用:

这意味着你不能使用静态url来引用图片(urllib.urlopen("https://notacarioca.rio.gov.br/senhaweb/CaptchaImage.aspx?guid=9759fc80-d385-480a-aa6e-8e00ef20be7b&s=1")不起作用)。
这是我使用Requests和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()https://stackoverflow.com/questions/41754219
复制相似问题