【问题标题】:How to download the image if we have given url?如果我们提供了 url,如何下载图像?
【发布时间】:2022-11-02 18:03:07
【问题描述】:

我有以下 URL,我想使用代码下载这些图像。有数百万个 URL,所以我想用 python 来做。

1) https://image.lexica.art/md/dbbb96f1-fce2-4970-ab62-b4b4e6859fe9
2) https://image.lexica.art/md/76318f25-5736-4cda-965d-96fe34823263
3) https://image.lexica.art/md/c11dd279-757e-43ff-8305-43e106f6c345
4) https://image.lexica.art/md/f38d92bb-99bc-4611-938f-c5d6cc70d6ea

我已经尝试了以下代码,但没有奏效。

url = 'https://image.lexica.art/md/76318f25-5736-4cda-965d-96fe34823263'
folder_path = 'images_artistics'
file_name = url.split('/')[-1][:-4]

image_content = requests.get(url).content
image_file = io.BytesIO(image_content)
image = Image.open(image_file).convert('RGB')
file_path = os.path.join(folder_path, file_name)
f = open(file_path, 'wb')
image.save(f, "JPEG", quality=85)
print(f"SAVED - {url} - AT: {file_path}")

我得到的错误;

【问题讨论】:

  • 请不要发布错误消息的(不完整的)图片 - 发布实际消息。请确保您的代码是完整的 - 其中包括必要的 import 语句。请查看您从request 收到的数据,以确保它至少以正确的图像魔术签名开头。如果您要下载数百万张图像,每张图像都有延迟,那么您将在那里等待很长时间,因此您需要考虑多处理或异步解决方案。

标签: python web-scraping python-imaging-library


【解决方案1】:

我会为此使用requests

import requests

url_list = [
    'https://image.lexica.art/md/dbbb96f1-fce2-4970-ab62-b4b4e6859fe9',
    'https://image.lexica.art/md/76318f25-5736-4cda-965d-96fe34823263',
    'https://image.lexica.art/md/c11dd279-757e-43ff-8305-43e106f6c345',
    'https://image.lexica.art/md/f38d92bb-99bc-4611-938f-c5d6cc70d6ea',
]

for url in url_list:
    filename = url.split('/')[-1]
    response = requests.get(url)
    open(filename, 'wb').write(response.content)

【讨论】:

  • 你能看到,反应是什么?我得到403。
  • 您是否使用请求下载了图像?尝试您的代码,然后下载图像。我不这么认为,它会工作
  • 该代码有效,我认为该页面由于缺少标题信息而拒绝访问。我发现这些是 webp 图像,所以如果下载有效,你可能仍然需要转换它们。稍后我会尝试弄清楚,也许我会找到一些东西
  • 确切地说,我会等待你的回应。
【解决方案2】:

相当复杂......从wireshark跟踪,我看到它正在使用HTTP2,我猜服务器也会检查类似浏览器的HTTP标头(例如“User-Agent”等)

确保使用 http2 pip3 install 'httpx[http2]' 安装 httpx

然后试试这个,

import httpx
from PIL import Image
from io import BytesIO
import httpx
import asyncio

async def main():
    url_list = [
        'https://image.lexica.art/md/dbbb96f1-fce2-4970-ab62-b4b4e6859fe9',
        #'https://image.lexica.art/md/76318f25-5736-4cda-965d-96fe34823263',
        #'https://image.lexica.art/md/c11dd279-757e-43ff-8305-43e106f6c345',
        #'https://image.lexica.art/md/f38d92bb-99bc-4611-938f-c5d6cc70d6ea',
    ]

    headers = {
        'Host': 'image.lexica.art',
        'authority': 'image.lexica.art',
        'method': 'GET',
        'scheme': 'https',
        'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image,webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
        'accept-encoding': 'gzip, deflate, br',
        'accept-language': 'en-US,en;q=0.9',
        'cache-control': 'max-age=0',
        'sec-ch-ua': '" Not A;Brand";v="99", "Chromium";v="102", "Google Chrome";v="102"',
        'sec-ch-ua-mobile': '?0',
        'sec-ch-ua-platform': 'macOS',
        'sec-fetch-dest': 'document',
        'sec-fetch-mode': 'navigate',
        'sec-fetch-site': 'none',
        'sec-fetch-user': '?1',
        'upgrade-insecure-requests': '1',
        'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36'
    }

    for url in url_list:
        filename = url.split('/')[-1]
        client = httpx.AsyncClient(http2=True)
        response = await client.get(url, headers=headers)
        image = Image.open(BytesIO(response.content)).convert('RGB')
        image.show()

if __name__ == "__main__":
    asyncio.run( main() )

祝你好运 !

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-12
    • 2019-12-24
    • 2014-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多