首页 > 解决方案 > 为什么我的解析图像链接以 base64 格式输出

问题描述

我试图解析来自网站的图像链接。当我检查网站上的链接时,它是这个:https ://static.nike.com/a/images/c_limit,w_592,f_auto/t_product_v1/df7c2668-f714-4ced-9f8f-1f0024f945a9/chaussure-de- Basketball-zoom-freak-3-MZpJZF.png但是当我用我的代码解析它时,输出是data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7.

from bs4 import BeautifulSoup
import requests

source = requests.get('https://www.nike.com/fr/w/hommes-chaussures-nik1zy7ok').text

soup = BeautifulSoup(source, 'lxml')

pair = soup.find('div', class_='product-card__body')

image_scr = pair.find('img', class_='css-1fxh5tw product-card__hero-image')['src']
print(image_scr)

我认为代码不是问题,但我不知道是什么导致链接以 base64 格式出现。那么我如何设置代码以将链接呈现为 .png ?

标签: pythonparsingbeautifulsoupbase64

解决方案


由于要抓取 src 意思是图片数据,所以使用请求从服务器下载数据,需要使用.content如下格式:

source = requests.get('https://www.nike.com/fr/w/hommes-chaussures-nik1zy7ok').content

推荐阅读