自动织布机开/关问题

2024-06-17 15:25:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经编写了一个在Windows32位机器上运行的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()

Tags: namelocalhosturldbiftime进程浏览器