noio
noio

Reputation: 5812

Controlling a browser from Python

I am looking for a way to control a browser from Python, i.e. fill out form fields and submit them, possibly call JS functions. I've looked around a bit, but as far as I could see PyWebKitGtk only lets you show the browser as a GUI element, not interface with it.

Is there a way to do this easily? I wrote my program logic in Python, and I would hate to port it to JS. Besides that, even if I'd use pure JS "bookmarklets", those wouldn't be able to read/write to my local filesystem, would they?

Also, some of the content on the page is generated using AJAX, so I'm looking for a solution where javascript runs as normal.

P.S. to quell your suspicions, I'm not trying to automatically fill out forum account creation forms or something similarly spammious, though the task is technically similar. I need to crawl/scrape sites for my research project.

EDIT: IEC looked promising, but I'm working on a Mac.

Upvotes: 2

Views: 3037

Answers (3)

Radek
Radek

Reputation: 3931

You can implement a Webkit browser (in Python):

import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtWebKit import *
app = QApplication(sys.argv)
web = QWebView()
web.load(QUrl("http://google.com"))
web.show()
sys.exit(app.exec_())

Or alternatively use Crowbar that will give you an API interface for a xulrunner (Firefox).

Upvotes: 1

Daniel Roseman
Daniel Roseman

Reputation: 599490

You might try something like Selenium, which is an application to script browsers through various languages including Python - it's meant for cross-browser testing, but might do what you want.

Upvotes: 3

rossipedia
rossipedia

Reputation: 59367

Why not just use Python to simulate a browser, parsing the HTML and building the appropriate HTTP requests as necessary?

A quick google yields the following:

Upvotes: 1

Related Questions