使用Selenium,请求和枕头下载图片
我正在关注一个教程,以使用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要下载图像,只需将
.content
从requests.get()
保存到二进制文件。例如:保存
test.jpg
:To download image, simply save the
.content
fromrequests.get()
to a binary file. For example:Saves
test.jpg
: