作者:绿色风
分类:
时间:2022-08-17
浏览:149
楼主 水星钓鱼 |
Q:如何针对ASP网页调用XMLHTTP查询数据? A:以下是其中一个范例:
- Sub xyf()
- Dim oXmlHttp As Object, ohtml As Object, obj1, obj2, obj3
- Dim sVS As String, sEV As String, sstr As String
- Dim iPage As Integer, TPage As Integer
- Set oXmlHttp = CreateObject("msxml2.xmlhttp")
- With oXmlHttp
- .Open "GET", "http://www.drugfuture.com/cndrug/search.aspx?SearchTerm=" & xyf_Escape("阿莫西林") & "&DataFieldSelected=auto", False
- .send
- Set ohtml = CreateObject("htmlfile")
- ohtml.body.innerhtml = .responsetext
- ' ohtml.body.innerhtml = VBA.StrConv(.responseBody, vbUnicode)
- sVS = ohtml.getElementById("__VIEWSTATE").Value
- sEV = ohtml.getElementById("__EVENTVALIDATION").Value
- TPage = ohtml.getElementById("ResultNational").getElementsbytagname("span")(0).innertext
- Set obj1 = ohtml.getElementById("GridViewNational")
- Set obj2 = obj1.Rows
- For i = 0 To obj2.Length - 2
- Set obj3 = obj2(i).Cells
- For j = 0 To obj3.Length - 1
- Cells(k + 1, j + 1) = obj3(j).innertext
- Next
- k = k + 1
- Next
- For iPage = 2 To Round(TPage \ 30)
- sstr = "__EVENTTARGET=GridViewNational&__EVENTARGUMENT=Page" & xyf_Escape("$" & iPage) & "&__LASTFOCUS=&__VIEWSTATE=" & xyf_EncodeURI(sVS) & "&__EVENTVALIDATION=" & xyf_EncodeURI(sEV)
- With oXmlHttp
- .Open "POST", "http://www.drugfuture.com/cndrug/search.aspx?SearchTerm=" & xyf_Escape("阿莫西林") & "&DataFieldSelected=auto", False
- .SetRequestHeader "Content-Type", "application/x-www-form-urlencoded"
- .send (sstr)
- ohtml.body.innerhtml = .responsetext
- sVS = ohtml.getElementById("__VIEWSTATE").Value
- sEV = ohtml.getElementById("__EVENTVALIDATION").Value
- ' ohtml.body.innerhtml = VBA.StrConv(.responseBody, vbUnicode)
- Set obj1 = ohtml.getElementById("GridViewNational")
- Set obj2 = obj1.Rows
- For i = 1 To obj2.Length - 2
- Set obj3 = obj2(i).Cells
- For j = 0 To obj3.Length - 1
- Cells(k + 1, j + 1) = obj3(j).innertext
- Next
- k = k + 1
- Next
- End With
- Next
- End With
- End Sub
- Function xyf_EncodeURI(str)
- Dim obj
- Set obj = CreateObject("ScriptControl")
- With obj
- .Language = "JavaScript"
- xyf_EncodeURI = .Eval("encodeURIComponent(""" & str & """)")
- End With
- Set obj = Nothing
- End Function
- Function xyf_Escape(str)
- Dim obj
- Set obj = CreateObject("ScriptControl")
- With obj
- .Language = "JavaScript"
- xyf_Escape = .Eval("escape(""" & str & """)")
- End With
- Set obj = Nothing
- End Function
原帖链接
- http://www.exceltip.net/forum.php?mod=viewthread&tid=51275&fromuid=111
|
免责声明
有感于原ExcelTip.Net留存知识的价值及部分知识具有的时间限定性因素,
经与ExcelTip.Net站长Apolloh商议并征得其同意,
现将原属ExcelTip.Net的知识帖采集资料于本站点进行展示,
供有需要的人士查询使用,也慰缅曾经的论坛时代。
所示各个帖子的原作者如对版权有异议,
可与本人沟通提出,或于本站点留言,我们会尽快处理。
在此,感谢ExcelTip.Net站长Apolloh的支持,感谢本站点所有人**绿色风(QQ:79664738)**的支持与奉献,特此鸣谢!
------本人网名**KevinChengCW(QQ:1210618015)**原ExcelTip.Net总版主之一