Python使用selenium下载百度图片
import os
from time import sleep
from selenium import webdriver
from selenium.webdriver.common.by import By
def init(): #初始化浏览器
save_path = r"D:\Deskbook\image" #下载路径
os.makedirs(save_path, exist_ok=True) # 确保输出文件夹存在
timeout = 2 #超时等待时间
url = "https://image.baidu.com/search/detail?" #网页url
#编辑配置
prefs = {
"profile.default_content_settings.popups": 0, #防止保存弹窗
"download.default_directory": save_path, #修改默认下载路径
"profile.default_content_setting_values.automatic_downloads": 1 #允许多文件下载
}
chrome_options = webdriver.ChromeOptions()
# 添加配置
chrome_options.add_experimental_option("prefs", prefs)
# 修改 windows.navigator.webdriver, 防机器人识别机制, selenium 自动登陆判别机制
chrome_options.add_experimental_option("excludeSwitches", ["enable-automation"])
# 禁用启用blink运行时的功能
chrome_options.add_argument("--disable-blink-features=AutomationControlled")
driver = webdriver.Chrome(options=chrome_options)
print("启动浏览器")
# 超时等待, 隐性等待
driver.implicitly_wait(timeout)
driver.get(url=url)
print("打开网页")
return driver
def find(driver,xp): #捕获网页元素
try:
element = driver.find_element(By.XPATH,xp)
return element
except Exception as e:
print(xp+" 捕获失败")
raise e
def work(driver):
driver.switch_to.default_content()
download = find(driver,'//*[@class="bar-btn btn-download"]') # 下载键
download.click()
sleep(1)
img_next = find(driver,'//*[@class="img-next"]') # 切换下一张图像
img_next.click()
if __name__ == "__main__":
driver = init()
for i in range(300):
work(driver)
driver.quit()