维普

xiaoxiao2021-02-28  137

#!/usr/bin/python #encoding=utf-8 __author__ = 'Administrator' from bs4 import BeautifulSoup import selenium import sys import urllib import requests import time import re if __name__ == "__main__": import os from selenium import webdriver from selenium.webdriver.support.ui import WebDriverWait headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:23.0) Gecko/20100101 Firefox/23.0'} chromedriver = "/home/henson/Documents/pycharm/webdriver/chromedriver" chromeOptions = webdriver.ChromeOptions() prefs = {"download.default_directory": "/home/henson/Downloads/vp/recover"} #下载路径 chromeOptions.add_experimental_option("prefs", prefs) os.environ["webdriver.chrome.driver"] = chromedriver driver = webdriver.Chrome(chromedriver,chrome_options=chromeOptions) driver.execute_script("window.scrollTo(0, document.body.scrollHeight);") #下载链接可改page+size driver.get('http://qikan.cqvip.com/zk/search.aspx?from=zk_search&key=U=生态治理与恢复&size=50&page=1#search-result-list') currentURL=driver.current_url time.sleep(2) k=1 for j in range(3, 300): #下载选页 driver.find_element_by_xpath('//*[@id="searchop"]/div[1]/a[1]').click() #全选 driver.find_element_by_xpath('//*[@id="searchop"]/div[1]/a[3]').click() #导出 now_handle = driver.current_window_handle # 获取当前窗口句柄 all_handles = driver.window_handles # 获取所有窗口句柄 time.sleep(2) for handle in all_handles: if (handle != now_handle): driver.switch_to_window(handle) #print("new web" + driver.current_url) driver.find_element_by_xpath('//*[@id="showinfobtn3"]').click() #XML driver.find_element_by_xpath('//*[@id="exportbtn"]').click() #开始导出 driver.close() print(now_handle) # 输出主窗口句柄 driver.switch_to_window(now_handle) # 返回主窗口 driver.find_element_by_xpath('//*[@id="searchop"]/div[1]/a[2]').click() #清除 driver.find_element_by_xpath( '//*[@id="body"]/div/div[3]/div[6]/div[2]/span[2]/a[2]').click() #下一页 time.sleep(2) currentURL = driver.current_url

用来获取跳转的当前窗口 now_handle = driver.current_window_handle # 获取当前窗口句柄 all_handles = driver.window_handles # 获取所有窗口句柄 设置timeout配合until来确定是否加载完成

转载请注明原文地址: https://www.6miu.com/read-36404.html

最新回复(0)