将目标表从HTML复制到Excel时未显示
尝试从HTML文件中选择下拉列表,并尝试将结果表复制到Excel中。但是下面的代码是HTML中的一些其他文本,而不是表格(我想填充到Excel)将目标表从HTML复制到Excel时未显示,excel,vba,internet-explorer,dom,web-scraping,Excel,Vba,Internet Explorer,Dom,Web Scraping,尝试从HTML文件中选择下拉列表,并尝试将结果表复制到Excel中。但是下面的代码是HTML中的一些其他文本,而不是表格(我想填充到Excel) 选项显式 'VBE>工具>参考: “Microsoft Internet控件 公共子生成选项() Dim ie作为InternetExplorer,ele作为Object,t作为Date 常量最大等待时间=5秒 暗商品如弦,暗商品如弦 商品=“中央邦” iDate=“REWA” Set ie=新的InternetExplorer 与ie .Visibl
选项显式
'VBE>工具>参考:
“Microsoft Internet控件
公共子生成选项()
Dim ie作为InternetExplorer,ele作为Object,t作为Date
常量最大等待时间=5秒
暗商品如弦,暗商品如弦
商品=“中央邦”
iDate=“REWA”
Set ie=新的InternetExplorer
与ie
.Visible=True
.导航2“http://hydro.imd.gov.in/hydrometweb/(S(3QITCIJD521EGPZHWQ3JK55))/DistrictRaifall.aspx“
当.Busy或.readyState<4:DoEvents:Wend时
.document.querySelector(“[value=”&commodity&“]”)。Selected=True
.document.querySelector(“[name=listItems]”)。FireEvent“onchange”
t=计时器
做
出错时继续下一步
Set ele=.document.querySelector(“[value=”&iDate&“]”)
错误转到0
如果定时器-t>最大等待时间,则退出Do
循环,而ele什么都不是
如果不是,那么ele什么都不是
ele.Selected=True
.document.querySelector(#GoBtn”)。单击
其他的
出口接头
如果结束
“停
”“退出
Dim doc作为对象、hTable作为对象、hBody作为对象、hTR作为对象、hTD作为对象
尺寸tb作为对象,bb作为对象,tr作为对象,td作为对象
将y标注为长,z标注为长,wb标注为Excel.工作簿,ws标注为Excel.Worksheet
设置wb=Excel.active工作簿
设置ws=wb.ActiveSheet
y=1'Excel中的A列
z=1'Excel中的第1行
Set doc=ie.document
Set hTable=doc.getElementsByTagName(“表”)
对于HTTable中的每个tb
设置hBody=tb.getElementsByTagName(“tbody”)
对于hBody中的每个bb
设置hTR=bb.getElementsByTagName(“tr”)
对于hTR中的每个tr
设置hTD=tr.getElementsByTagName(“td”)
y=1'重置回A列
对于hTD中的每个td
ws.Cells(z,y).Value=td.innerText
y=y+1
下一个td
多芬特
z=z+1
下一个tr
退出
下一个bb
退出
下一个结核病
以
端接头
我会使用剪贴板和定时循环。还有重新分解的余地,我稍后会做
该页面对结果执行xhr POST请求,因此请使用您可以复制的开发工具。它使用以下url:。见最后的评论
Option Explicit
'VBE > Tools > References:
' Microsoft Internet Controls
Public Sub MakeSelections()
Dim ie As InternetExplorer, commodity As String, iDate As String, clipboard As Object, arr()
Set clipboard = GetObject("New:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")
Set ie = New InternetExplorer
commodity = "MADHYA PRADESH"
iDate = "REWA"
With ie
.Visible = True
.Navigate2 "http://hydro.imd.gov.in/hydrometweb/(S(3qitcijd521egpzhwqq3jk55))/DistrictRaifall.aspx"
While .Busy Or .readyState < 4: DoEvents: Wend
.document.querySelector("[value='" & commodity & "']").Selected = True
.document.querySelector("[name=listItems]").FireEvent "onchange"
arr = ElementFound(.document, "[value='" & iDate & "']")
If Not arr(0) Then Exit Sub
arr(1).Selected = True
.document.querySelector("#GoBtn").Click
arr = ElementFound(.document, "#GridId")
If Not arr(0) Then Exit Sub
clipboard.SetText arr(1).outerHTML
clipboard.PutInClipboard
ThisWorkbook.Worksheets("Sheet1").Cells(1, 1).PasteSpecial
.Quit
End With
End Sub
Public Function ElementFound(ByRef document As Object, ByVal selector As String) As Variant
Dim ele As Object, t As Date, arr(0 To 1)
Const MAX_WAIT_SEC As Long = 5
t = Timer
Do
On Error Resume Next
Set ele = document.querySelector(selector)
On Error GoTo 0
If Timer - t > MAX_WAIT_SEC Then Exit Do
Loop While ele Is Nothing
If Not ele Is Nothing Then
arr(0) = True
Else
arr(0) = False
End If
Set arr(1) = ele
ElementFound = arr
End Function
选项显式
'VBE>工具>参考:
“Microsoft Internet控件
公共子生成选项()
Dim ie作为InternetExplorer,商品作为字符串,iDate作为字符串,剪贴板作为对象,arr()
设置剪贴板=GetObject(“新建:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}”)
Set ie=新的InternetExplorer
商品=“中央邦”
iDate=“REWA”
与ie
.Visible=True
.导航2“http://hydro.imd.gov.in/hydrometweb/(S(3QITCIJD521EGPZHWQ3JK55))/DistrictRaifall.aspx“
当.Busy或.readyState<4:DoEvents:Wend时
.document.querySelector(“[value=”&commodity&“]”)。Selected=True
.document.querySelector(“[name=listItems]”)。FireEvent“onchange”
arr=ElementFound(.document,“[value=”&id&“]”)
如果不是arr(0),则退出Sub
arr(1)。所选=真
.document.querySelector(#GoBtn”)。单击
arr=ElementFound(.document,#GridId)
如果不是arr(0),则退出Sub
剪贴板.SetText arr(1).outerHTML
剪贴板.PutInClipboard
此工作簿。工作表(“表1”)。单元格(1,1)。粘贴特殊
退出
以
端接头
找到的公共函数元素(ByRef文档作为对象,ByVal选择器作为字符串)作为变量
尺寸元素作为对象,t作为日期,arr(0到1)
常量最大等待时间=5秒
t=计时器
做
出错时继续下一步
Set ele=document.querySelector(选择器)
错误转到0
如果定时器-t>最大等待时间,则退出Do
循环,而ele什么都不是
如果不是,那么ele什么都不是
arr(0)=真
其他的
arr(0)=假
如果结束
设置arr(1)=ele
ElementFound=arr
端函数
XHR: POST主体包括以下参数: __EVENTTARGET:vbNullString __EVENTARGUMENT:vbNullString __LASTFOCUS:vbNullString __视图状态:待获取。在我的情况下,至少从之前的GET中获得这一点是不起作用的 __VIEWSTATEGENERATOR:6C290774'<静态,但可以从先前的GET获取 列表项:中央邦“将空格替换为”+ 地区下拉列表:REWA
GoBtn:GO您可以尝试通过其ID查找HTML表,然后尝试循环遍历其行以获取数据,这可能有助于获取正确的数据。再次感谢你,哈瑞!它工作得很好。帮了大忙!
Option Explicit
'VBE > Tools > References:
' Microsoft Internet Controls
Public Sub MakeSelections()
Dim ie As InternetExplorer, commodity As String, iDate As String, clipboard As Object, arr()
Set clipboard = GetObject("New:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")
Set ie = New InternetExplorer
commodity = "MADHYA PRADESH"
iDate = "REWA"
With ie
.Visible = True
.Navigate2 "http://hydro.imd.gov.in/hydrometweb/(S(3qitcijd521egpzhwqq3jk55))/DistrictRaifall.aspx"
While .Busy Or .readyState < 4: DoEvents: Wend
.document.querySelector("[value='" & commodity & "']").Selected = True
.document.querySelector("[name=listItems]").FireEvent "onchange"
arr = ElementFound(.document, "[value='" & iDate & "']")
If Not arr(0) Then Exit Sub
arr(1).Selected = True
.document.querySelector("#GoBtn").Click
arr = ElementFound(.document, "#GridId")
If Not arr(0) Then Exit Sub
clipboard.SetText arr(1).outerHTML
clipboard.PutInClipboard
ThisWorkbook.Worksheets("Sheet1").Cells(1, 1).PasteSpecial
.Quit
End With
End Sub
Public Function ElementFound(ByRef document As Object, ByVal selector As String) As Variant
Dim ele As Object, t As Date, arr(0 To 1)
Const MAX_WAIT_SEC As Long = 5
t = Timer
Do
On Error Resume Next
Set ele = document.querySelector(selector)
On Error GoTo 0
If Timer - t > MAX_WAIT_SEC Then Exit Do
Loop While ele Is Nothing
If Not ele Is Nothing Then
arr(0) = True
Else
arr(0) = False
End If
Set arr(1) = ele
ElementFound = arr
End Function