selenium 如何下载所有照片?

q35jwt9p  于 2022-12-23  发布在  其他
关注(0)|答案(1)|浏览(95)

为什么我的代码只下载了16张图片?
enter link description here

driver.get(url)
driver.implicitly_wait(50)
for x in range(7):
    driver.execute_script("window.scrollBy(0, 200)")
    sleep(0.5)

driver.find_element(By.CLASS_NAME, "facetwp-load-more").click()

counter = 1
for i in     driver.find_elements(By.XPATH,"/html/body/div[6]/div[2]/div/div/div/div/div/div[1]/a/div/img"): 
    atributoSrc = i.get_attribute("src")
    file_name = f"image{counter:02d}.jpg"
    imagem.append(atributoSrc)
    urllib.request.urlretrieve(atributoSrc,f"C:\\__Imagens e Planilhas     Python\\Facebook\\Imagens\\ImagensJPG\\{file_name}") 
    counter += 1
oknwwptz

oknwwptz1#

我创建了一个https://github.com/seleniumbase/SeleniumBase脚本,将所有这些图像作为文件下载:

import os
from seleniumbase import BaseCase

if __name__ == "__main__":
    from pytest import main
    main([__file__])

class MyTestClass(BaseCase):
    def test_base(self):
        self.open("https://focastock.com/?s=food")
        img_elements_with_src = self.find_elements("img[src]")
        unique_src_values = []
        for img in img_elements_with_src:
            src = img.get_attribute("src")
            if src not in unique_src_values:
                unique_src_values.append(src)
        print()
        for src in unique_src_values:
            if src.split(".")[-1] not in ["png", "jpg", "jpeg"]:
                continue
            self.download_file(src)  # Goes to downloaded_files/
            filename = src.split("/")[-1]
            self.assert_downloaded_file(filename)
            folder = "downloaded_files"
            file_path = os.path.join(folder, filename)
            self._print(file_path)

已成功下载所有图像。

相关问题