在網上看到梅勁松同學關於操作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()