PhantomJS 已经不再开发了,Seleniumn 也警告使用 PhantomJS 是过时的,推荐使用 headless 版的 Chrome 或者 Firefox,但是有时候需要用到,够用就行,而且在 Linux 下安装也相对简单。
安装 fontconfig 依赖
1
| yum install -y fontconfig freetype freetype-devel fontconfig-devel libstdc++
|
下载 PhantomJS 并解压
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| cd /usr/local
wget https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-2.1.1-linux-x86_64.tar.bz2
tar -jxvf phantomjs-2.1.1-linux-x86_64.tar.bz2
mv phantomjs-2.1.1-linux-x86_64 phantomjs
ln -s /usr/local/phantomjs/bin/phantomjs /usr/bin/phantomjs
phantomjs --version
|
用 Sselenium 驱动 PhantomJS
1 2 3 4 5 6 7 8 9 10
| from selenium import webdriver from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
dcap = dict(DesiredCapabilities.PHANTOMJS) dcap["phantomjs.page.settings.userAgent"] = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36" driver = webdriver.PhantomJS(desired_capabilities=dcap) driver.set_page_load_timeout(10) driver.set_script_timeout(10) driver.get("https://www.baidu.com")
|
推荐使用的 Chrome 用法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| from selenium import webdriver from selenium.webdriver.chrome.options import Options
options = Options() options.add_argument('headless') options.add_argument('disable-gpu') options.add_argument('window-size=1200x600')
prefs = {'profile.managed_default_content_settings.javascript': 2} options.add_experimental_option("prefs", prefs)
prefs = {"profile.default_content_setting_values.notifications": 2} options.add_experimental_option("prefs", prefs)
prefs = {'profile.managed_default_content_settings.images': 2} options.add_experimental_option("prefs", prefs)
driver = webdriver.Chrome(chrome_options=options)
driver.execute_script('window.scrollTo(0, 0)') driver.execute_script('window.scrollTo(0, document.body.scrollHeight)')
|