參考博文https://www.cnblogs.com/shaosks/p/7278634.html html
使用selenium的webdriver配合chrome實現了js頁面的解析web
配置了chrome×××面啓動一直未生效chrome
運行腳本發現啓動chrome和登陸過程都很是慢不明白緣由框架
import datetimeless
import seleniumide
from selenium import webdriverurl
from selenium.webdriver.chrome.options import Optionsspa
from time import sleepcode
from pyquery import PyQuery as pqhtm
host=["https://192.40.0.6/iPages/i_login.asp","https://192.40.0.7/iPages/i_login.asp","https://192.40.0.8/iPages/i_login.asp","https://192.40.0.9/iPages/i_login.asp","https://192.40.0.10/iPages/i_login.asp",
"https://192.40.1.6/iPages/i_login.asp","https://192.40.1.7/iPages/i_login.asp","https://192.40.1.8/iPages/i_login.asp","https://192.40.1.9/iPages/i_login.asp","https://192.40.1.10/iPages/i_login.asp"]
for url in host:
opt = webdriver.ChromeOptions()
opt.set_headless()
browser = webdriver.Chrome(options=opt)
browser.get(url)
browser.find_element_by_id("username").send_keys("admin")
browser.find_element_by_id("password").send_keys("Passw0rd")
browser.find_element_by_id("loginButton").click()
sleep(1)
browser.switch_to.frame('mainFrame') #處理頁面框架的切換
#sleep(1)
browser.switch_to.frame('treeFrame')
selenium_html = browser.execute_script("return document.documentElement.outerHTML") #返回頁面的html元素
doc = pq(selenium_html)
dochtml = doc('a[id="Open_Problems_anchor"]')
strSources = dochtml.text().encode('utf8').strip()
txtsource = strSources[strSources.find('Open Problems') + 0:] #提取指定元素的html
print (datetime.datetime.now().strftime('%m-%d %H;%M:%S'),url,txtsource)
browser.close()