我想用VBA(Excel)解析一长串本地网页(.HTM
文件),然后将一些数据提取到excel中。该程序需要抓取9000多个网页。这是一个例子:
> C:\Users\User_ID\Webpages\BS_1000.HTM.htm
C:\Users\User_ID\Webpages\BS_1001.HTM.htm
C:\Users\User_ID\Webpages\BS_1002.HTM.htm
C:\Users\User_ID\Webpages\BS_1003.HTM.htm
C:\Users\User_ID\Webpages\BS_1006.HTM.htm
C:\Users\User_ID\Webpages\BS_1007.HTM.htm
C:\Users\User_ID\Webpages\BS_1011.HTM.htm
C:\Users\User_ID\Webpages\BS_1012.HTM.htm
C:\Users\User_ID\Webpages\BS_1015.HTM.htm
C:\Users\User_ID\Webpages\BS_1016.HTM.htm
[... and the list goes on ...]
这是VBA:
<!-- language: lang-HTML -->
For startNumber = 1 To TotalProfiles
Dim ie As InternetExplorerMedium
Set ie = New InternetExplorerMedium
ie.Visible = True
Application.StatusBar = "Loading profile " & ProfileNumber & " from a total of " & TotalProfiles & " profiles"
Set currentProfile = Worksheets("List_of_Files").Range("B" & CurrentRowPosition)
ie.navigate currentProfile
Application.StatusBar = "Loading profile: " & ProfileNumber & "; file location: " & currentProfile
Do While ie.READYSTATE <> READYSTATE_COMPLET
DoEvents
Loop
Application.StatusBar = "Storing " & currentProfile & " information into HTMLElement"
Set html = ie.document
Set ie = Nothing
[some code here...]
问题是我当前的代码在新的IE窗口中打开了每个页面(而没有关闭前一个窗口)。要抓取9000多个网页,这很快就会成为一个很大的问题。
我将Internet Explorer 11(在Windows 7 Enterprise SP1上)与Microsoft Office 2013一起使用。
我想要的是IE应该在同一个标签页中打开每个网页(完成分析后,只需刷新已在使用中的标签页并加载下一页-或至少在完成分析后关闭窗口并打开下一个网页即可)在“新”窗口中)。可悲的是,直到现在我才设法找到解决方案。任何帮助,将不胜感激。
每次打开一个新窗口的原因是,您在此循环的开头将此行告诉它 Set ie = New InternetExplorerMedium
有两种解决方法。
像这样:
Dim ie As InternetExplorerMedium
Set ie = New InternetExplorerMedium
ie.Visible = True
For startNumber = 1 To TotalProfiles
Application.StatusBar = "Loading profile: " & ProfileNumber & "; file location: " & currentProfile
Do While ie.READYSTATE <> READYSTATE_COMPLET
DoEvents
Loop
Set currentProfile = Worksheets("List_of_Files").Range("B" & CurrentRowPosition)
ie.navigate currentProfile
Application.StatusBar = "Storing " & currentProfile & " information into HTMLElement"
Set html = ie.document
[some code here...]
Next
Set html = Nothing
ie.Quit
Set ie = Nothing
像这样:
For startNumber = 1 To TotalProfiles
Dim ie As InternetExplorerMedium
Set ie = New InternetExplorerMedium
ie.Visible = True
Application.StatusBar = "Loading profile " & ProfileNumber & " from a total of " & TotalProfiles & " profiles"
Set currentProfile = Worksheets("List_of_Files").Range("B" & CurrentRowPosition)
ie.navigate currentProfile
Application.StatusBar = "Loading profile: " & ProfileNumber & "; file location: " & currentProfile
Do While ie.READYSTATE <> READYSTATE_COMPLET
DoEvents
Loop
Application.StatusBar = "Storing " & currentProfile & " information into HTMLElement"
Set html = ie.document
[some code here...]
Set html = Nothing
ie.Quit
Set ie = Nothing
Next
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句