在Python中将Headless Firefox与Selenium结合使用



Headless Firefox and regular Firefox have the same capabilities, and running them with Selenium is a very similar process. The difference is that Headless Firefox does not generate any sort of user interface. In other words, no browser is visibly launched.

无头Firefox和常规Firefox具有相同的功能,并且使用Selenium运行它们是非常相似的过程。 区别在于,Headless Firefox不会生成任何类型的用户界面。 换句话说,没有浏览器被启动。

If you happen to be web scraping with Selenium, it’s often helpful to see what exactly the browser is doing in real time for development and debugging purposes. However, using headless mode can be great if your script is working and you don’t want to be bothered with an open browser. And even better, a headless browser should generally run faster than its headed counterpart, given that it doesn’t require the extra resources normally needed to visually render everything happening in the browser.

如果您碰巧是使用Selenium进行Web抓取,通常对于实时查看浏览器在进行开发和调试时所做的工作通常很有帮助。 但是,如果您的脚本正在运行并且不想被打开的浏览器所困扰,那么使用无头模式可能会很棒。 甚至更好的是,无头浏览器通常应比同类浏览器运行得更快,因为它不需要通常所需的额外资源来可视化呈现浏览器中发生的所有事情。

To run Headless Firefox, you’ll first need to set up Selenium.

要运行Headless Firefox,首先需要设置Selenium

Once you’ve got Selenium working, using Headless Firefox is a breeze. For example, let’s see if we can get to DuckDuckGo‘s home page.

一旦Selenium正常运行,使用Headless Firefox变得轻而易举。 例如,让我们看看是否可以访问DuckDuckGo的主页。

Since Headless Firefox has no visible browser, we’ll take a screenshot to confirm what the browser is doing.

由于无头Firefox没有可见的浏览器,因此我们将截屏以确认浏览器在做什么。

from selenium import webdriver

geckodriver = 'C:UsersgraysonDownloadsgeckodriver.exe'

options = webdriver.FirefoxOptions()
options.add_argument('-headless')

browser = webdriver.Firefox(executable_path=geckodriver, firefox_options=options)

browser.get('https://www.duckduckgo.com')

browser.save_screenshot('C:UsersgraysonDownloadsheadless_firefox_test.png')

browser.quit()

翻译自: https://www.pybloggers.com/2018/03/using-headless-firefox-with-selenium-in-python/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值