通过 BeautifulSoup 从网页下载图像数据 URI

Ris*_*hav 0 python beautifulsoup python-2.7

我需要使用 Python 从网站检索图像。但是,该图像不是链接文件的形式,而是 GIF 数据 URI。如何下载该文件并将其存储在 .gif 文件中?

niv*_*xer 8

这应该会让你朝着正确的方向前进。

首先,我假设您已经检索了图像 uri 数据,并将其保存在名为 img_data 的 python 变量中:

# Example
img_data = 'data:image/jpeg;base64,/9j/4A...<lots of data>...k='
Run Code Online (Sandbox Code Playgroud)

现在您需要从 Base64 解码图片并将其保存到文件中:

import base64

# Separate the metadata from the image data
head, data = img_data.split(',', 1)

# Get the file extension (gif, jpeg, png)
file_ext = head.split(';')[0].split('/')[1]

# Decode the image data
plain_data = base64.b64decode(data)

# Write the image to a file
with open('image.' + file_ext, 'wb') as f:
    f.write(plain_data)
Run Code Online (Sandbox Code Playgroud)