【发布时间】:2015-01-08 23:57:50
【问题描述】:
下面的代码(基于 -Using VBA in Excel to Google Search in IE and return the hyperlink of the first result,@Santosh 编写)似乎为几乎所有 URL 打印了 getelementsbytagname("a")。事实证明它不适用于某些人,并且在代码 (weatherford.com) 中给出了示例 URL ...除非我注释掉 Zz 行。有什么原因吗?
注意:要打印 carmax 的链接,请按原样运行代码。要打印weatherford 的链接,请注释掉第1a 和2a 行,取消注释第1b 和2b 行。运行宏,它将为weatherford 链接打印一个空白的.txt 文档。现在删除桌面上的weatherfordlinks.txt文件,注释掉Zz行并运行宏...它现在将打印weatherford链接。
Sub testxmlhttp()
Dim xmlHttp As Object, myURL As String, html As Object, lnk As Object, links As Object
myURL = "http://www.carmax.com/" '-->1a
'myURL = "http://www.weatherford.com" '-->1b
Set xmlHttp = CreateObject("MSXML2.serverXMLHTTP")
xmlHttp.Open "GET", myURL, False
xmlHttp.setRequestHeader "Content-Type", "text/xml" '-->Zz
xmlHttp.setRequestHeader "User-Agent", "Mozilla/5.0 (Windows NT 6.1; rv:25.0) Gecko/20100101 Firefox/25.0"
On Error Resume Next
xmlHttp.Send
Set html = CreateObject("htmlfile")
On Error Resume Next
html.body.innerHTML = xmlHttp.responseText
Open "C:\Users\~\desktop\carmaxLinks.txt" For Output As #1 '-->2a
'Open "C:\Users\~\desktop\weatherfordLinks.txt" For Output As #1 '-->2b
For Each lnk In html.getelementsbytagname("a")
Print #1, lnk
Next
Close #1
End Sub
【问题讨论】:
标签: excel vba xmlhttprequest