在 Python 中每次迭代将图片保存到新文件夹
Saving pictures to a new folder each iteration in Python
我在 Python 和 Selenium 库中练习我的网络抓取技能。我的目标是从房地产网站 (comparis.ch) 下载图片。我有一个 link 列表,其中每个 link 是一个公寓。我想将每个公寓的照片保存在每个 link 的新文件夹中(例如公寓 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:
我在 Python 和 Selenium 库中练习我的网络抓取技能。我的目标是从房地产网站 (comparis.ch) 下载图片。我有一个 link 列表,其中每个 link 是一个公寓。我想将每个公寓的照片保存在每个 link 的新文件夹中(例如公寓 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: