使用Selenium,请求和枕头下载图片

发布于 2025-02-04 17:51:32 字数 1012 浏览 1 评论 0原文

我正在关注一个教程,以使用Google Chrome的Python下载图像。我正在下载一张图像的第一步,但是它没有下载任何内容,但是它确实打印了成功。当我给我一条路径时,以及当我离开这样的路径时“”将其下载到我的IDE中时,我什么也没得到。

这是代码:

import requests
from PIL import Image
from selenium import webdriver
import io

PATH = "/Users/flaviamadau/Desktop/SchoolPython/chromedriver"

wd = webdriver

driver = webdriver.Chrome(executable_path=PATH)

image_url = "https://target.scene7.com/is/image/Target/GUEST_34ac5146-0911-4c3f-ae9f-473d268ad847"
download_paths= "/Users/flaviamadau/Desktop/SchoolPython/fotos"

def download_image(download_path, url, file_name):
    try:
        image_content = requests.get(url).content
        image_file= io.BytesIO(image_content)
        image = Image.open(image_file)
        file_path = download_path + file_name

        with open(file_path, "wb") as f:
            image.save(f, "JPEG")

        print("Success")
    except Exception as e:
        print('Failed -', e)

download_image(download_paths, image_url, "test.jpg")

任何帮助或反馈都非常感谢!

I'm following a tutorial to download images with python of google chrome. I'm in the first step of downloading one image but it's not downloading anything however it does print Success. Both when I give a path and also when I leave the path like " " this to download it within my IDE I get nothing.

this is the code:

import requests
from PIL import Image
from selenium import webdriver
import io

PATH = "/Users/flaviamadau/Desktop/SchoolPython/chromedriver"

wd = webdriver

driver = webdriver.Chrome(executable_path=PATH)

image_url = "https://target.scene7.com/is/image/Target/GUEST_34ac5146-0911-4c3f-ae9f-473d268ad847"
download_paths= "/Users/flaviamadau/Desktop/SchoolPython/fotos"

def download_image(download_path, url, file_name):
    try:
        image_content = requests.get(url).content
        image_file= io.BytesIO(image_content)
        image = Image.open(image_file)
        file_path = download_path + file_name

        with open(file_path, "wb") as f:
            image.save(f, "JPEG")

        print("Success")
    except Exception as e:
        print('Failed -', e)

download_image(download_paths, image_url, "test.jpg")

any help or feedback is greatly appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

银河中√捞星星 2025-02-11 17:51:32

要下载图像,只需将.contentrequests.get()保存到二进制文件。例如:

import requests


url = "https://target.scene7.com/is/image/Target/GUEST_34ac5146-0911-4c3f-ae9f-473d268ad847"

with open("test.jpg", "wb") as f_out:
    f_out.write(requests.get(url).content)

保存test.jpg

andrej@andrej:~/$ ls -l test.jpg
-rw-r--r-- 1 root root 8008 june  6 14:20 test.jpg

To download image, simply save the .content from requests.get() to a binary file. For example:

import requests


url = "https://target.scene7.com/is/image/Target/GUEST_34ac5146-0911-4c3f-ae9f-473d268ad847"

with open("test.jpg", "wb") as f_out:
    f_out.write(requests.get(url).content)

Saves test.jpg:

andrej@andrej:~/$ ls -l test.jpg
-rw-r--r-- 1 root root 8008 june  6 14:20 test.jpg
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文