在网上看到梅劲松同学关于操作IE的代码,
http://blog.csdn.net/meijingsong/archive/2004/09/14/103624.aspx
代码清楚明了,唯一不足是最后程序会一直卡在pythoncom.PumpMessages()
在网上google了一会,找以相应的解决方法,在此贴出,给需要的同学:(红色代码为增加的退出pythoncom.PumpMessages之用
)
# -*- coding: cp936 -*-
import win32gui
import win32com
import win32com.client
import pythoncom
import time
class EventHandler:
def OnVisible(self, visible):
global bVisibleEventFired
bVisibleEventFired = 1
def OnDownloadBegin(self):
print "DownloadBegin"
#先继承全局变量增加一个字符串
global testlist
testlist.append("DownloadBegin")
def OnDownloadComplete(self):
print "DownloadComplete"
#先继承全局变量增加一个字符串
global testlist
testlist.append("DownloadComplete")
def OnDocumentComplete(self, pDisp = pythoncom.Missing , URL = pythoncom.Missing):
print "documentComplete of %s" % URL
#先继承全局变量再打印
global testlist
print testlist
global ie
ie.ExecWB(45,1)
import win32api
win32api.PostQuitMessage()
class runcom:
def __init__(self):
global testlist
global ie
ie = win32com.client.DispatchWithEvents("InternetExplorer.Application", EventHandler)
ie.Visible = 1
ie.Navigate("www.aawns.com")
#打印全局变量
print testlist
pythoncom.PumpMessages()
#ie.Quit()
print "End"
testlist=[]
ie = None
a=runcom()