首页 > 解决方案 > 在 Python 中每次迭代都将图片保存到新文件夹

问题描述

我在 Python 和 Selenium 库中练习我的网页抓取技巧。我的目标是从房地产网站 (comparis.ch) 下载图像。我有一个链接列表,其中每个链接都是一个公寓。我想将每个公寓的照片保存在每个链接的新文件夹中(如公寓 1、公寓 2 ...)。无法弄清楚如何做到这一点,也许有人可以提供帮助,我对 Python 还是很陌生。谢谢 ;)

for link in links:
    url = link
    driver.get(url)

    # scraping pictures from the website
    # finding number of grey circles that indicate photos
    circles = len(driver.find_elements_by_class_name("svg-inline--fa.fa-circle.fa-w-16.css- 
    1xkwzfp"))+1
    print("{} photos found at the website.".format(circles))

    # creating a set, since duplicates are excluded in the set
    images_urls = set()
    for n in range(circles):
        # finding image containers
        images_containers = driver.find_element_by_class_name("css- 
        ze3zoq").find_elements_by_tag_name("img")

    for image in images_containers:
        # scraping urls from containers and store it in a set to avoid duplicates
        images_urls.update([image.get_attribute("src")])
        # click to scroll photos to the right and thus upload more photos
        driver.find_element_by_class_name("css-11m3oda.excbu0j2").click()

    # download photos (HOW TO SAVE THEM TO A NEW FOLDER EACH TIME?)
    for i in images_urls:
        # Download the images using requests library
        with open("C:/Users/potek/Jupyter_projects/apartments/{}".format("Comparis"+str(time.time())+".jpg"), "wb") as f: # comment 
        f.write(requests.get(i).content)```

标签: pythonseleniumweb-scrapingdirectorycreate-directory

解决方案


将第 1 行编辑为:

for num, link in enumerate(links):

将此附加到第 26 行(在 之后for i in images_urls):

path = "C:/Users/potek/Jupyter_projects/apartments{}".format(num)
os.makedirs(path) # don't forgate to import os

编辑with open("C:/Users/potek/Jupyter_projects/apartments/{}".format("Comparis"+str(time.time())+".jpg"), "wb") as f:

with open(os.path.join(path, "Comparis"+str(time.time())+".jpg"), "wb") as f:

推荐阅读