使用PhantomJS运行Selenium Web Scrap大约需要3.5秒才能返回数据点(网页上一个<span>
元素的内容),但是要打开Firefox的--headless
选项并运行相同的代码要花30多秒的时间。由于不再支持PhantomJS,有没有办法让我使用无头模式来加速程序?代码如下所示。
# Using PhantomJS
driver = webdriver.PhantomJS()
driver.get("https://finance.yahoo.com/quote/" + ticker + "?p=" + ticker + "&.tsrc=fin-srch")
price_locator = driver.find_element_by_css_selector('.Fz\(36px\)')
price = price_locator.text
driver.close()
return price
# Headless Mode
options = Options()
options.add_argument('--headless')
driver = webdriver.Firefox(options=options)
driver.get("https://finance.yahoo.com/quote/" + ticker + "?p=" + ticker + "&.tsrc=fin-srch")
price_locator = driver.find_element_by_css_selector('.Fz\(36px\)')
price = price_locator.text
driver.close()
return price