Embed a web browser in a Python program

How can I embed a web browser in a Python program? It needs to run on Linux (GTK, Qt are fine), or cross-platform.

I have looked at embedding pywebgtk and Qt's WebKit widget. But these seem to have little more than a rendering engine. In particular, I'd like support for back/forward and tabbed browsing. Is something like this pre-packaged, or do I have to implement it myself?

wxWebConnect seems to be roughly what I was thinking of, but it has no Python bindings.


http://pypi.python.org/pypi/selenium/2.7.0

You can install the selenium package and run a server (same machine, just a different process) with it which you connect to with your python code:

java -jar selenium-server-standalone-2.7.0.jar

then:

from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.keys import Keys
import time

browser = webdriver.Firefox() # Get local session of firefox
browser.get("http://www.yahoo.com") # Load page
assert "Yahoo!" in browser.title
elem = browser.find_element_by_name("p") # Find the query box
elem.send_keys("seleniumhq" + Keys.RETURN)
time.sleep(0.2) # Let the page load, will be added to the API
try:
    browser.find_element_by_xpath("//a[contains(@href,'http://seleniumhq.org')]")
except NoSuchElementException:
    assert 0, "can't find seleniumhq"
browser.close()

You could use subprocess to start the server inside your python code.

链接地址: http://www.djcxy.com/p/55038.html

上一篇: 石英:内存泄漏?

下一篇: 在Python程序中嵌入一个Web浏览器