0

I've written a Python script running on a windows 32bit machine that will query a mysql database and then proceed to open a webbrowser with the relevant url and after 15 seconds it will terminate in a very crude way the browser process and it does this for each entry in the result set.

Is there a nicer way to load a page, wait for the page to fully load and then close the browser instead of using time.sleep() and terminating the browser process so crudely?

import webbrowser,time,subprocess,pymysql

# open db connection
db = pymysql.connect(host="localhost", user="dev", passwd="password", db="testdb")

# create cursor and execure SQL statement
cursor = db.cursor()
cursor.execute("SELECT name, id FROM test_table")

# commit your changes if any
db.commit()

# get number of rows in resultset
numrows = int(cursor.rowcount)

# loop through resultset
for x in range(0, numrows):
    row = cursor.fetchone()

    url = ''

    if (row[1] == 1):
        url = 'http://localhost/?name=' + row[0]

    if (row[1] == 2):
        url = 'http://localhost/?func=2&name=' + row[0]

    webbrowser.open_new(url)
    time.sleep(15)
    subprocess.call("taskkill /IM iexplore.exe /f /t", shell=True)
    time.sleep(15)

cursor.close()
db.close()
llanato
  • 2,508
  • 6
  • 37
  • 59
  • 5
    you could try `selenium webdriver` to drive the browser (it allows you to fine-tune your definition of what it means that a page is loaded). Why do you need to open a web-browser? – jfs Oct 24 '14 at 14:08
  • @J.F.Sebastian, there is data on the page that is only loaded via Javascript which I need to retrieve. – llanato Oct 24 '14 at 14:19
  • 1
    here's code example: [Getting all visible text from a webpage using Selenium](http://stackoverflow.com/q/7947579/4279). – jfs Oct 24 '14 at 14:24
  • 1
    @Tall have a look on the java script. Maybe its better to figure out which url they query. Maybe they will give you some nice json or xml response which you could parse without hassle. This aproach will be a lot faster... – enthus1ast Oct 24 '14 at 15:02

0 Answers0