python - 在 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)```
解决方案
将第 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:
推荐阅读
- c++ - 将变量传递到 CURLOPT_POSTFIELDS c++
- python - 为什么 K.gradients(loss, input_img)[0] 在 Kears CNN 中返回“None”?
- sql - 我的日期筛选器在 SQL Server 中无法正常工作
- javascript - 为什么在Angular 9项目中将自定义标志传递给nodejs命令行时出现“节点:错误选项”错误
- python - 芹菜,关系“x_x”不存在
- ios - RxDataSources 在数据源内容的每次更改时闪烁
- javascript - 如何解决electron的setInterval无效问题?
- java - 在 Kotlin 中按顺序打开 Intent
- arrays - 在这段代码中是否有另一种方式来表示 fscanf 的第三个参数?
- html - 我可以有一个页面,单击图像时单击显示带有密码的新空白页面,提交显示另一个带有文件uload请求提示的空白页面