自动webbrowser打开/关闭问题

时间:2014-10-24 14:00:58

标签: python python-2.7 browser

我已经编写了一个在Windows 32位机器上运行的Python脚本,它将查询一个mysql数据库,然后继续打开一个带有相关url的webbrowser,15秒后它将以非常粗暴的方式终止浏览器进程它为结果集中的每个条目执行此操作。

有没有更好的方法来加载页面,等待页面完全加载然后关闭浏览器而不是使用time.sleep()并如此粗暴地终止浏览器进程?

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()

0 个答案:

没有答案
相关问题