所以我一直试图使用下面的代码从网站上获取信息,但是我正在努力处理我所注意到的部分。有没有更简单的方法来获得我想要的结果,而不是一遍又一遍地重复相同的代码?你永远不知道会得到多少结果所以我不能使用那些信息。
Dim objIE As InternetExplorer
Dim y As Integer
Dim result As String
Set objIE = New InternetExplorer
objIE.Visible = True
objIE.navigate "https://www.searchiqs.com/nyalb/Login.aspx"
Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop
objIE.document.getElementById("btnGuestLogin").Click
Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop
objIE.document.getElementById("ContentPlaceHolder1_txtFromDate").Value = "07/01/2017"
objIE.document.getElementById("ContentPlaceHolder1_txtThruDate").Value = "07/19/2017"
objIE.document.getElementById("ContentPlaceHolder1_cboDocGroup").Value = "DBA"
objIE.document.getElementById("ContentPlaceHolder1_cmdSearch").Click
Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop
y = 1
' brings up a table of results. I need to click on the view button and get the info from that page
objIE.document.getElementById("ContentPlaceHolder1_grdResults_btnView_0").Click
Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop
' element ID's are hidden for those looking to help
result = Trim(objIE.document.getElementById("ContentPlaceHolder1_lblDetails2").innerText)
Sheets("Sheet1").Range("C" & y).Value = result
y = y + 1
' I would love to direct link to the next serch result here but the link does not change between pages so I click the next button
objIE.document.getElementById("ContentPlaceHolder1_btnNext").Click
Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop
Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop
result = Trim(objIE.document.getElementById("ContentPlaceHolder1_lblDetails2").innerText)
Sheets("Sheet1").Range("C" & y).Value = result
y = y + 1
' and I click the next button again
objIE.document.getElementById("ContentPlaceHolder1_btnNext").Click
Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop
Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop
result = Trim(objIE.document.getElementById("ContentPlaceHolder1_lblDetails2").innerText)
Sheets("Sheet1").Range("C" & y).Value = result
y = y + 1
'AND AGAIN
objIE.document.getElementById("ContentPlaceHolder1_btnNext").Click
Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop
Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop
result = Trim(objIE.document.getElementById("ContentPlaceHolder1_lblDetails2").innerText)
Sheets("Sheet1").Range("C" & y).Value = result
y = y + 1
' You get the idea
objIE.document.getElementById("ContentPlaceHolder1_btnNext").Click
Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop
Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop
result = Trim(objIE.document.getElementById("ContentPlaceHolder1_lblDetails2").innerText)
Sheets("Sheet1").Range("C" & y).Value = result
' I basically repeat that process til I cant press CTRL V anymore. Is there a loop I can use to tighten this up?
让我知道。你们这些人过去都是一个巨大的帮助。使用XML方法也是有益的,因为我的readystate循环给了我一些问题?提前谢谢。
答案 0 :(得分:0)
是的,如果它是一组静态页面,则为所需迭代次数的For循环。我相信我们可以让它变得更简单。
For y = 1 to 5
result =trim(objIE.document.getElementById("ContentPlaceHolder1_lblDetails2").innerText)
Sheets("Sheet1").Range("C" & y).Value = result
objIE.document.getElementById("ContentPlaceHolder1_btnNext").Click
Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop
next y
'or if you want it to keep going, something like this
Do until objIE.document.getElementById("ContentPlaceHolder1_btnNext") is nothing
result =trim(objIE.document.getElementById("ContentPlaceHolder1_lblDetails2").innerText)
Sheets("Sheet1").Range("C" & y).Value = result
objIE.document.getElementById("ContentPlaceHolder1_btnNext").Click
Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop
loop