如何从 JavaScript 呈现的响应页面下载最高分辨率的图像?

How do I download the highest resolution image from a JavaScript rendered responsive page?

假设这是网站页面:“https://www.dior.com/en_us/products/couture-943C105A4655_C679-technical-fabric-cargo-pants-covered-in-tulle”,我想从中下载所展示产品的所有图片(在本例中为 4 张图片)。

我正在使用 Selenium 并提取图像链接。 问题是,如果我单击图像,它们甚至有 2000x3000 像素大,但我只能获得它们的 480 像素分辨率图像。这些图像存储在哪里?我如何提取它们? (基本上我想下载那些图像的最大可能大小)

根据您提供的页面源代码,json 数据提供了页面的链接和内容。一旦从源代码中的脚本中剥离数据,就可以轻松检索高分辨率链接并下载图像。如果您还没有,pip install requestspip install bs4.

import requests, re, json
from bs4 import BeautifulSoup

url = 'https://www.dior.com/en_us/products/couture-943C105A4655_C679-technical-fabric-cargo-pants-covered-in-tulle'

r = requests.get(url)
soup = BeautifulSoup(r.text, 'lxml')
script = [script.text for script in soup.find_all('script') if 'window.initialState' in script.text][0]
json_data_s = re.search(r'{.+}', script).group(0)
json_data = json.loads(json_data_s)
for holder in json_data['CONTENT']['cmsContent']['elements']:
    if holder.get('type') == 'PRODUCTMEDIAS':
        for image in holder['items']:
            name = image['galleryImages']['imageZoom']['viewCode']
            img_src = image['galleryImages']['imageZoom']['uri']
            image_page = requests.get(img_src)
            with open(name + '.jpg', 'wb') as img:
                img.write(image_page.content)

*您之前下载的图片是缩略图。