selenium驅動chrome
阿新 • • 發佈:2018-11-30
直接上程式碼,之後解釋
#coding:utf-8
import time
from bs4 import BeautifulSoup
from selenium import webdriver
path = "/Users/yh/workshop/xplace/temp/chromedriver"
chrome_options = webdriver.chrome.options.Options()
chrome_options.set_headless(headless=True)
driver=webdriver.chrome.webdriver.WebDriver(executable_path=path,options=chrome_options)
driver.maximize_window()
# driver.implicitly_wait(10)
driver.get('https://en.wikipedia.org/wiki/Unexpected_hanging_paradox')
time.sleep(2)
out = driver.execute_script("return document.getElementsByTagName('html')[0].innerHTML;")
# out = driver.execute_script("return document.title")
print("out....",out)
# body = driver.find_element_by_tag_name('body')
# print(body.text)
# driver.implicitly_wait(400)
# html = driver.page_source
# soup = BeautifulSoup(html, "lxml")
# for tag in soup.find_all():
# if tag.name in ["script"]:
# tag.decompose()
# else:
# print(tag.name)
driver.quit()