calling a simple PyQt application more than once
jjposner at optimum.net
Sun Jan 29 18:23:58 EST 2012
Jabba Laci wrote:
> Hi, Thanks for your reply. I forgot to mention that my first solution
> created a headless browser, i.e. it didn't create any GUI. I would
> like to keep it that way, thus I could scrape (AJAX-powered) webpages
> in batch mode without any user interaction.
No head, no problem. Just use a QCoreApplication instead of a
QApplication, and use a QTimer instead of a QPushButton to invoke
from PyQt4.QtCore import QUrl, QCoreApplication, QTimer
from PyQt4.QtNetwork import QNetworkAccessManager, QNetworkRequest
resp = reply_obj.readAll()
req = QNetworkRequest(QUrl(MYURL))
MYURL = 'http://simile.mit.edu/crowbar/test.html'
if __name__ == "__main__":
# we need only one application object and one net-access mgr
app = QCoreApplication()
mgr = QNetworkAccessManager()
# use timer instead of button to retrieve web page repeatedly
timer = QTimer()
timer.start(5 * 1000)
# start the event loop
Another thought: if you don't need a GUI, you might consider using plain
old Python, rather than PyQt. Here's some code adapted from
WEBPAGE = "http://simile.mit.edu/crowbar/test.html"
SEVERAL_TIMES = 3
for _ in range(SEVERAL_TIMES):
# Get a file-like object for the Python Web site's home page.
f = urllib.urlopen(WEBPAGE)
# Read from the object, storing the page's contents in 's'.
s = f.read()
More information about the Python-list