calling a simple PyQt application more than once

John Posner 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
do_click():

from PyQt4.QtCore import QUrl, QCoreApplication, QTimer
from PyQt4.QtNetwork import QNetworkAccessManager, QNetworkRequest

def process_page(reply_obj):
    resp = reply_obj.readAll()
    reply_obj.close()
    print str(resp).strip()

def do_click():
    req = QNetworkRequest(QUrl(MYURL))
    mgr.finished.connect(process_page)
    mgr.get(req)

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.timeout.connect(do_click)
    timer.start(5 * 1000)
 
    # start the event loop
    app.exec_()

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
http://www.boddie.org.uk/python/HTML.html:

import urllib

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()
    f.close()
    print s

Best,
John




More information about the Python-list mailing list