使用 Python 控制浏览器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3369073/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Controlling Browser using Python?
提问by demos
Is it possible to control a web browser like Firefox using Python?
是否可以使用 Python 控制像 Firefox 这样的网络浏览器?
I would want to do things like
我想做这样的事情
- launch the browser
- force clicks on URLs
- take screenshots
- 启动浏览器
- 强制点击 URL
- 截屏
etc.
等等。
回答by Tim McNamara
Selenium Remote Controlis a project that comes very close to what you are after. It is really easy to get working in Python with the selenium.webdriversubpackage that comes with it. Once upon a time, these were two projects. They've now been unified.
Selenium Remote Control是一个非常接近您所追求的项目。使用 Python 附带的selenium.webdriver子包可以很容易地使用它。曾几何时,这是两个项目。他们现在已经统一了。
Installation
安装
Simple!
简单的!
$ pip install -U selenium
Usage
用法
>>> from selenium import webdriver
>>> ff = webdriver.Firefox()
>>> ff.get("http://stackoverflow.com/q/3369073/395287")
>>> ff.save_screenshot("/absolute/path/to/webpage.png")
Notes
笔记
The documentation can be slightly confusing for Selenium, because there are two modes to interact with browsers. As well as the webdrivermode, there is the ability to talk to a "standalone Selenium Remote Control server". That approach is what is documented first in the official documentation, but I would stick with webdriverfor the simple task here.
Selenium 的文档可能有点混乱,因为有两种模式可以与浏览器交互。除了webdriver模式,还有与“独立的 Selenium 远程控制服务器”对话的能力。这种方法是官方文档中首先记录的方法,但我会坚持webdriver这里的简单任务。
回答by Katriel
wxWebConnectis a wxWidgets library for controlling Gecko (Mozilla's rendering engine). Together with wxPython, it would allow you to write your own (minimal) web browser in Python, and hence control clicks.
wxWebConnect是一个 wxWidgets 库,用于控制 Gecko(Mozilla 的渲染引擎)。与wxPython一起,它将允许您用 Python 编写自己的(最小)Web 浏览器,从而控制点击。
回答by Daniel Kluev
Depends what do you actually want to achieve. If you need to do some automatic stuff w/out user interference, you can just use underlying engine of the browser, like Gecko or WebKit, w/out loading browser itself. There are ready Python bindings to these engines available.
取决于你真正想要达到的目标。如果你需要在没有用户干扰的情况下做一些自动的事情,你可以使用浏览器的底层引擎,比如 Gecko 或 WebKit,而无需加载浏览器本身。这些引擎有现成的 Python 绑定可用。
Browsers themself do not provide this kind of API to outside processes. For Firefox, you would need to inject some browser-side code into chrome, either as extension or plugin.
浏览器本身不向外部进程提供这种 API。对于 Firefox,您需要将一些浏览器端代码作为扩展或插件注入 chrome。
回答by Guillaume Lebourgeois
Ag great way to control a browser in Python is to use PyQt4.QtWebKit.
Ag 在 Python 中控制浏览器的好方法是使用PyQt4.QtWebKit.
回答by marbdq
If you need to take screenshots, then you need to render the pages. I would recommend to use Selenium (as mentioned by Tim), or then spynner.
如果您需要截屏,则需要渲染页面。我会建议使用 Selenium (如 Tim 提到的),然后使用spynner。
Here is a sample code of what you need using spynner:
以下是使用 spynner 所需的示例代码:
import spynner
browser = spynner.Browser()
browser.load("http://stackoverflow.com/q/3369073/")
browser.snapshot().save('file.png')
browser.close()
回答by tokland
The question is a bit old but I see no references to pywebkitgtk, you should give it a try. I developed spynnerusing the pyqt.qtwebkitbindings, but programmers who prefer pygtk should try pywebketgtk (which feels more pythonic to me). Note though that Qt bindings are more complete (or at least it was last time I checked).
这个问题有点老了,但我看不到对pywebkitgtk 的引用,你应该试一试。我公司开发spynner使用pyqt.qtwebkit绑定,但谁愿意pygtk的程序员应该尽量pywebketgtk(这感觉更Python对我来说)。请注意,尽管 Qt 绑定更完整(或者至少是我上次检查时)。

