简体   繁体   English

使用 python 将下载的图像保存到本地文件夹中

[英]Save downloaded images with python into a local folder

I use the following code to scrape image url from a website.我使用以下代码从网站上抓取图像 url。 What I try to achieve next is to save urls that end with '.jpg' into a local folder which can be the location of the py code.我接下来尝试实现的是将以“.jpg”结尾的 url 保存到本地文件夹中,该文件夹可以是 py 代码的位置。 I manage to scrape and access urls and to create a folder in that location but I don't know how to save them.我设法抓取和访问网址并在该位置创建一个文件夹,但我不知道如何保存它们。 This is my code, any ideas are highly appreciated这是我的代码,任何想法都非常感谢

from selenium import webdriver
import requests
import os
from bs4 import BeautifulSoup
import urllib
import urllib.request
from urllib.request import urlretrieve
import sys

if sys.version_info[0] >= 3:

from urllib.request import urlretrieve
else:
# if Not Python 3
from urllib import urlretrieve

site = 'https://www.amazon.de/dp/B077S8N26F'
directory = os.path.dirname(os.path.realpath(__file__)) + '/image_folder/'
if not os.path.exists(directory):
    os.makedirs(directory)

driver = webdriver.Chrome()
driver.get(site)
soup = BeautifulSoup(driver.page_source, 'html.parser')

img_tags = soup.find_all('img')
urls = [img['src'] for img in img_tags]
for url in urls:
print(url)
#only the links that end with .jpg
images = [im for im in urls if im.endswith(".jpg")]
print(images)
for im in images:
   #here is the missing part that saves urls into the folder created

For each of your entries in images , that'll be the URI/URL of an image file.对于images中的每个条目,这将是图像文件的 URI/URL。

To get the image, you need to make a separate HTTP request to get it.要获取图像,您需要单独发出 HTTP 请求来获取它。 You can do this with python-requests你可以用python-requests做到这一点

This existing answer should help you along the way without repeating it: How to download image using requests这个现有的答案应该可以帮助您,无需重复: 如何使用请求下载图像

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM