使用excel vba进行刮取在刮取前更改输入数据
在我尝试更改网站上输入字段的数据,并在页面上刷新该信息。我已经更新了输入字段,但我不确定如何刷新页面,以便内部表使用输入字段中的新数据 下面是我的代码:使用excel vba进行刮取在刮取前更改输入数据,excel,vba,web-scraping,inputbox,Excel,Vba,Web Scraping,Inputbox,在我尝试更改网站上输入字段的数据,并在页面上刷新该信息。我已经更新了输入字段,但我不确定如何刷新页面,以便内部表使用输入字段中的新数据 下面是我的代码: Dim IE As InternetExplorer Dim htmldoc As HTMLDocument Dim ieURL As String Dim sPicker As String ieURL = "https://www.investing.com/commodities/crude-oil-historical-data"
Dim IE As InternetExplorer
Dim htmldoc As HTMLDocument
Dim ieURL As String
Dim sPicker As String
ieURL = "https://www.investing.com/commodities/crude-oil-historical-data"
sPicker = "10/01/2017 - 12/31/2017"
'Open InternetExplorer
Set IE = New InternetExplorer
IE.Visible = True
IE.Navigate ieURL
Set htmldoc = IE.document 'Document webpage
' wait until the page loads before doing anything
Do Until (IE.readyState = 4 And Not IE.Busy)
DoEvents ' DoEvents releases the macro and lets excel do other thing while it waits
Loop
Dim drp As HTMLFormElement
Set drp = htmldoc.getElementById("widgetFieldDateRange")
drp.innerText = sPicker 'Set the new timeframe for scraping
Dim inpt As HTMLInputElement
Set inpt = htmldoc.getElementById("picker")
inpt.Value = sPicker 'Set the new timeframe for scraping
' wait until the page loads before doing anything
Do Until (IE.readyState = 4 And Not IE.Busy)
DoEvents ' DoEvents releases the macro and lets excel do other thing while it waits
Loop
感谢您的帮助尝试下面的脚本。它应该解决这个问题
Sub Web_Data()
Dim IE As New InternetExplorer, html As New HTMLDocument
Dim post As Object, elem As Object, t_data As Object
Dim trow As Object, tcel As Object
With IE
.Visible = True
.navigate "https://www.investing.com/commodities/crude-oil-historical-data"
While .readyState < 4: DoEvents: Wend
Set html = .document
End With
Application.Wait Now + TimeValue("00:00:05")
html.getElementById("widgetFieldDateRange").Click
Application.Wait Now + TimeValue("00:00:03")
Set post = html.getElementById("startDate")
post.innerText = ""
post.Focus
Application.SendKeys "10/01/2017"
Application.Wait Now + TimeValue("00:00:03")
Set elem = html.getElementById("endDate")
elem.innerText = ""
elem.Focus
Application.SendKeys "12/31/2017"
Application.Wait Now + TimeValue("00:00:03")
html.getElementById("applyBtn").Click
Application.Wait Now + TimeValue("00:00:03")
Set t_data = html.getElementById("curr_table")
For Each trow In t_data.Rows
For Each tcel In trow.Cells
y = y + 1: Cells(x + 1, y) = tcel.innerText
Next tcel
y = 0
x = x + 1
Next trow
End Sub
你试过下面的脚本@EddiRae吗?你的反馈是什么?Shahin,我注意到一旦代码完成,它会返回到主页(),然后我才能抓取刚刚用新日期更新的表。还有,还有,在IE不可见的情况下,还有没有其他方法可以执行SendKey?当页面被隐藏时,它似乎用日期而不是网站字段来更新代码。我已经更新了上面的脚本,用于解析新填充表中的数据。你错了@EddiRae。我检查了自己,根据你在帖子中要求的日期,我在电子表格中找到了正确的数据。执行上述脚本并亲自查看。至于你关心的第二个问题:我不知道如何使用IE来达到同样的效果。
1. Microsoft Internet Controls
2. Microsoft HTML Object Library