云计算、AI、云原生、大数据等一站式技术学习平台

网站首页 > 教程文章 正文

批量下载某photo网站图片(批量下载页面图片插件)

jxf315 2025-04-11 00:03:22 教程文章 13 ℃

from selenium import webdriver

from selenium.webdriver.common.by import By

from selenium.webdriver.support.ui import WebDriverWait

from selenium.webdriver.support import expected_conditions as EC

from selenium.webdriver.common.action_chains import ActionChains

from selenium.webdriver.common.keys import Keys

import time

import os

import requests

driver = webdriver.Chrome()

driver.get('https://')

# 创建ActionChains对象,按键使用

actions = ActionChains(driver)

time.sleep(30)

driver.implicitly_wait(30)

num_of_str=0

num_of_str=len(driver.find_elements_by_xpath("//li[contains(@class, 'photo-item')]"))

print(num_of_str)

if num_of_str==0:

print("html open error,NOT FIND picture")

exit()

sfind=driver.find_elements_by_xpath("//li[contains(@class, 'photo-item')]")

old_window = driver.current_window_handle

for ii in range(157,num_of_str):

file_name1 = "bb" + str(ii) + "html"

file_path1 = os.path.join("d:\\pic\\", file_name1)

file_name2 = "gif" + str(ii) + ".jpg"

file_path2 = os.path.join("d:\\pic\\", file_name2)

tfind="//li[contains(@class, 'photo-item')]["+str(ii)+"]"

print(tfind)

kk=driver.find_element_by_xpath(tfind)

print(kk)

print(kk.text,"")

kk.click()

time.sleep(20)

# 获取当前页面的HTML

new_window = driver.current_window_handle

page_html = driver.page_source

print(page_html)

link_element = driver.find_element_by_xpath('//li[i[@class="iconImg chakan"]]//a')

link_url= link_element.get_attribute('href')

print(link_url)

# 使用requests库获取链接指向的网页内容

response = requests.get(link_url)

# 检查请求是否成功

if response.status_code == 200:

# 将图片写入文件

with open(file_path2, 'wb') as file:

file.write(response.content)

print("图片已入文件"+file_path2)

#driver.close()

# 发送ESC键

actions.send_keys(Keys.ESCAPE).perform()

time.sleep(5)

driver.switch_to.window(old_window)

print("returen old window")

else:

print(f"无法访问链接 {view_original_link},状态码:

Tags:

最近发表
标签列表