Excel 如何使用VBA改进数据刮取?
我有下面的代码,它从内部网获取数据。但是获取数据需要更多的时间。有人能帮我修改代码以提高性能吗。 提前谢谢 注意-我没有发布URL,因为它是客户的网站。很抱歉Excel 如何使用VBA改进数据刮取?,excel,vba,web-scraping,Excel,Vba,Web Scraping,我有下面的代码,它从内部网获取数据。但是获取数据需要更多的时间。有人能帮我修改代码以提高性能吗。 提前谢谢 注意-我没有发布URL,因为它是客户的网站。很抱歉 Sub FetchData() Dim IE As Object Dim Doc As HTMLDocument Dim myStr As String On Error Resume Next Set IE = CreateObject("InternetExplorer.Application") 'SetBrowser
Sub FetchData()
Dim IE As Object
Dim Doc As HTMLDocument
Dim myStr As String
On Error Resume Next
Set IE = CreateObject("InternetExplorer.Application") 'SetBrowser
IE.Visible = False
IE.navigate "URL" 'Open website
Do While IE.Busy Or IE.ReadyState <> 4
Application.Wait DateAdd("s", 1, Now)
Loop
Set Doc = IE.Document
Doc.getElementById("tbxUserID").Value = InputBox("Please Enter Your ID")
Doc.getElementById("txtPassword").Value = InputBox("Please Enter Your
Password")
Doc.getElementById("BtnLogin").Click
Do While IE.Busy Or IE.ReadyState <> 4
Application.Wait DateAdd("s", 1, Now)
Loop
IE.navigate "URL"
Do While IE.Busy Or IE.ReadyState <> 4
Application.Wait DateAdd("s", 1, Now)
Loop
Dim LastRow As Long
Set wks = ActiveSheet
LastRow = wks.Cells(wks.Rows.Count, "A").End(xlUp).Row
Set rowNo = wks.Range("A1:A" & LastRow)
For rowNo = 2 To LastRow
Doc.getElementById("txtField1").Value =
ThisWorkbook.Sheets("Sheet1").Range("A" & rowNo).Value
Doc.getElementById("CtrlQuickSearch1_imgBtnSumbit").Click
Do While IE.Busy Or IE.ReadyState <> 4
Application.Wait DateAdd("s", 1, Now)
Loop
strVal1 = Doc.querySelectorAll("span")(33).innerText
ThisWorkbook.Sheets("Sheet1").Range("B" & rowNo).Value = strVal1
strVal2 = Doc.querySelectorAll("span")(35).innerText
ThisWorkbook.Sheets("Sheet1").Range("C" & rowNo).Value = strVal2
Next
End Sub
Sub-FetchData()
模糊的物体
作为HTMLDocument的Dim Doc
将myStr变暗为字符串
出错时继续下一步
设置IE=CreateObject(“InternetExplorer.Application”)“SetBrowser
可见=假
IE.navigate“URL”打开网站
在忙或准备状态4时执行
Application.Wait DateAdd(“s”,1,Now)
环
设置文档=即文档
Doc.getElementById(“tbxUserID”).Value=InputBox(“请输入您的ID”)
Doc.getElementById(“txtPassword”).Value=InputBox(“请输入您的
密码“)
Doc.getElementById(“BtnLogin”)。单击
在忙或准备状态4时执行
Application.Wait DateAdd(“s”,1,Now)
环
IE.导航“URL”
在忙或准备状态4时执行
Application.Wait DateAdd(“s”,1,Now)
环
最后一排一样长
设置wks=ActiveSheet
LastRow=wks.Cells(wks.Rows.Count,“A”).End(xlUp).Row
设置行号=wks.Range(“A1:A”和LastRow)
对于第2行到最后一行
Doc.getElementById(“txtField1”)。值=
ThisWorkbook.Sheets(“Sheet1”).Range(“A”)和rowNo.Value
Doc.getElementById(“CtrlQuickSearch1\u imgBtnSumbit”)。单击
在忙或准备状态4时执行
Application.Wait DateAdd(“s”,1,Now)
环
strVal1=Doc.queryselectoral(“span”)(33).innerText
ThisWorkbook.Sheets(“Sheet1”).Range(“B”)和rowNo.Value=strVal1
strVal2=Doc.queryselectoral(“span”)(35).innerText
ThisWorkbook.Sheets(“Sheet1”).Range(“C”和rowNo)。Value=strVal2
下一个
端接头
无法保证此操作将运行。注意事项:
工作表
收集选项Explicit
-这意味着您必须在整个过程中使用正确的数据类型。当前您有未声明的变量,例如,rowNo用作Long和range李>
继续下一步
.document.queryselectoral(“span”)(33)
,您可能会查找是否存在可以使用的单节点短选择器,而不是使用节点列表选项显式
公共子获取数据()
Dim ie作为对象,ie作为InternetExplorer
Dim lastRow为长,wks为工作表,i为长,ws为工作表
设置ie=New SHDocVw.InternetExplorer'SetBrowser
设置ws=ThisWorkbook.Worksheets(“Sheet1”)
设置WKS= ActudieSeTe',如果您的代码工作,请考虑张贴,而不是堆栈溢出感谢回放。我在“.Document.getElementById(“txtField1”).Value=loopvalues(i)”行上遇到运行时错误“424”,我应该将第二个URL放在代码中的什么位置?在“newURL=.Document.URL”行中,您可能需要一个定时循环(首先尝试在该行之前添加等待或使用F8单步执行-424是否消失?如果没有,请使用F8单步执行,并在本地窗口中检查loopValues(i)的值。它将A1值作为“loopValues(i)”。实际上,在再次登录页面之后,我必须导航到具有不同页面的同一网站,该页面包含元素“txtField1”和CtrlQuickSearch1_imgBtnSumbit。在代码中,我应该将第二个URL放在哪里?我说在哪里。navigate2 newurlging变量未定义在“ws.Range”(“B”&rowNo)。Value=val1”行上,并且在第行出现自动错误“val1=.Document.querySelectorAll(“span”)(33).innerText”
Option Explicit
Public Sub FetchData()
Dim ie As Object, ie As InternetExplorer
Dim lastRow As Long, wks As Worksheet, i As Long, ws As Worksheet
Set ie = New SHDocVw.InternetExplorer 'SetBrowser
Set ws = ThisWorkbook.Worksheets("Sheet1")
Set wks = ActiveSheet '<==use explicit sheet name if possible
lastRow = wks.Cells(wks.rows.Count, "A").End(xlUp).Row
loopvalues = Application.Transpose(wks.Range("A2:A" & lastRow).Value)
With ie
.Visible = False
.Navigate2 "URL" 'Open website
While .Busy Or .readyState < 4: DoEvents: Wend
.document.getElementById("tbxUserID").Value = InputBox("Please Enter Your ID")
.document.getElementById("txtPassword").Value = InputBox("Please Enter Your Password")
.document.getElementById("BtnLogin").Click
While .Busy Or ie.readyState < 4: DoEvents: Wend
Dim newURL As String, val1 As String, val2 As String
newURL = .document.URL
For i = LBound(loopvalues) To UBound(loopvalues)
.document.getElementById("txtField1").Value = loopvalues(i)
.document.getElementById("CtrlQuickSearch1_imgBtnSumbit").Click
While .Busy Or .readyState < 4: DoEvents: Wend
val1 = .document.querySelectorAll("span")(33).innerText
ws.Range("B" & i).Value = val1
val2 = .document.querySelectorAll("span")(35).innerText
ws.Range("C" & i).Value = val2
.Navigate2 newURL
While .Busy Or ie.readyState < 4: DoEvents: Wend
Next
.Quit
End With
End Sub