pip3 install seleniumpython
sudo apt-get install chromium-chromedriverweb
dpkg -L chromium-chromedriverchrome
3 import time 4 from datetime import datetime 5 6 from selenium import webdriver 7 from selenium.webdriver.chrome.options import Options 8 9 #from dbfs.dalog import log_print as print 10 11 12 class WebDriver(object): - 13 def __init__(self): - 14 self._options = Options() 2 15 self._options.add_argument('--headless') | 16 | 17 | 18 def get_web(self, url): - 19 content = '' 2 20 st = datetime.now() 2 21 with webdriver.Chrome(chrome_options=self._options) as driver: 2 22 print(">>>>> WebDriverChrom {} GET: {}\r\n".format(st, url)) 2 23 driver.get(url) 2 24 time.sleep(10) 2 25 content = driver.page_source 2 26 et = datetime.now() 2 27 print("<<<<< {} UseTime: {}\r\n <<< RECV {}".format(et, (et-st).total_seconds(), True)) 2 28 #print(content, driver.current_url) 2 29 return dict(status=True, content=content, url=driver.current_url) 30 31 32 webget = WebDriver().get_web 33 34 35 __all__ = ['webget'] 36 37 38 if __name__ == '__main__': - 39 url = 'https://kuaibao.qq.com/s/20181224A0R6M300?refer=kb_news&omgid=1e32392c176538c757f9132b230e79a2&chlid=5915889&atype=0' | 40 webget(url)
確保你已經安裝了 chromium。
chromium 安裝以後的目錄可使用 ls /usr/lib/chromium-browser 查看。
使用 sudo apt search chromedriver 能夠驚喜地發如今 APT 中就能夠安裝 chromium 驅動。
使用 sudo apt install -y chromium-chromedriver 便可安裝.less
使用方式:ui
$ python3 ...msg... >>> from selenium import webdriver >>> browser = webdriver.Chrome( ... executable_path='/usr/lib/chromium-browser/chromedriver') >>> browser.get('baidu.com') >>> browser.quit() >>> >>> exit()