使用VBA进行网页报废:无法填写表格

时间:2015-11-13 15:10:22

标签: vba excel-vba post web-scraping excel

我是博士生,我一直在训练自己使用VBA进行网络抓取几个月,所以我开始尝试通过它。我非常感谢stackoverflow上的帮助。但我现在坚持这个!

我一直在尝试从http://www.sem-o.com/MarketData/Pages/DynamicReports.aspx下载数据(需要免费帐户)。我设法选择报告和火灾事件,但在尝试输入日期时,我无法使其工作。我尝试了getelementbyid,getelementsbytagname,甚至是document.all都没有成功,我的想法已经用完(以及找到答案的关键字)。 这个page解释了表单的get和post方法之间的区别。在这种情况下,post方法似乎在HTML中使用,但我仍然无法找到它。

这是我到目前为止编写的代码:

Sub acces()
Dim ie As Object
Dim link As String
Dim startdate As Date
Dim enddate As Date
link = "http://www.sem-o.com/MarketData/Pages/DynamicReports.aspx"
startdate = #1/1/2014#
enddate = #3/31/2014#

For i = startdate To enddate
Y = Year(i)
If Len(Day(i)) = 1 Then
Daynum = "0" & Day(i)
ElseIf Len(Day(i)) = 2 Then
Daynum = Day(i)
End If
If Len(Month(i)) = 1 Then
MonthNum = "0" & Month(i)
ElseIf Len(Day(i)) = 2 Then
MonthNum = Month(i)
End If

Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
ie.navigate (link)

newHour = Hour(Now())
newMinute = Minute(Now())
newSecond = Second(Now()) + 3
waitTime = TimeSerial(newHour, newMinute, newSecond)
Application.Wait waitTime

Set SelectElem = ie.document.all("ctl00$ctl00$g_f5e6fa98_faa2_4210_85e9_780934d96ab8$cmbSelectReport")
SelectElem.Value = "/SEMO Dynamic Reports/Dynamic Reporting - Tables/Energy Data/System Frequency"
SelectElem.FireEvent ("onchange")

newHour = Hour(Now())
newMinute = Minute(Now())
newSecond = Second(Now()) + 3
waitTime = TimeSerial(newHour, newMinute, newSecond)
Application.Wait waitTime

'here where I cannot make it work

Set ObjTable = ie.document.all("input")
While a < ObjTable.Length

If x = ObjTable(a).all("ctl00_ctl00_g_9ab92c0a_eb10_4b6c_ad1b_7277cbdab462_prm_GetFromDate_prm_GetFromDateDate") Then
x = MonthNum & "/" & Daynum & "/" & Y
End If
a = a + 1
Wend
Next
End Sub

如果您对如何找到合适的商品有任何想法来填写日期,那将会有很大帮助!实际上,我需要长时间下载几个报告,如果要求整整一个月,网站会发现数据系列很重...

非常感谢!

0 个答案:

没有答案