我在 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)```
uj5u.com熱心網友回復:
將第 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:
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/345290.html
