话不多说直接上代码:
from selenium import webdriver
from selenium.webdriver import ActionChains
import time
import os
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
#创建浏览器驱动
driver = webdriver.Chrome()
url = 'http://www.*****.com'
driver.get(url)
#按页码进入下载详情页
for i in range(1,9):
driver.execute_script('window.scrollTo(0,document.body.scrollHeight)') # 滑到底部
#睡眠等待进入详情页下载
time.sleep(1)
#最大化浏览页面
driver.maximize_window()
#进入详情页,鼠标点击事件,下载当前页内PDF
for j in range(1, 11):
j = str(j)
click = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, '/html/body/div[5]/div/div[1]/div[2]/table/tbody/tr[' + j + ']/td[1]/div/a')))
ActionChains(driver).click(click).perform()
#定位当前网页
driver.switch_to.window(driver.window_handles[1])
time.sleep(3)
#点击PDF下载按钮
download = driver.find_element_by_link_text('PDF下载')
ActionChains(driver).click(download).perform()
#关闭当前页
driver.close()
#返回招标公告首页
driver.switch_to.window(driver.window_handles[0])
# if i < 4 and i > 968:
try:
input_box = driver.find_element_by_xpath('//*[@id="layui-laypage-'+str(i)+'"]/span[3]/input')
#清空文本框并输入页码
input_box.clear()
input_box.send_keys(i+1)
my_element = driver.find_element_by_class_name('layui-laypage-btn')
#下载到i页
print(i)
ActionChains(driver).click(my_element).perform()
except:
input_box = driver.find_element_by_xpath('//*[@id="layui-laypage-' + str(i) + '"]/span[4]/input')
# 清空文本框并输入页码
input_box.clear()
input_box.send_keys(i + 1)
my_element = driver.find_element_by_class_name('layui-laypage-btn')
# 下载到i页
print(i)
ActionChains(driver).click(my_element).perform()
根据实际网页具体情况具体分析,灵活替换
|