VBAparsingdom来find一个特定的href值

使用Excel VBA,由于所有法语版本的URL都在.xls文件中,因此我必须从同一站点的英文版本中find大约400个URL。

知道该网站的dom结构,我知道我可以:

  • 打开网页(MSXML2.XMLHTTP)
  • 在网页的标题中search一个特定的链接。 使用户能够切换语言的链接。 在那个链接(href)下,我将能够find英文链接,这就是我所需要的。
  • 之后,我将设法将该结果复制到工作表中的相应单元格中。

结构是这样的。 “英语”链接改变,但始终在这种结构下:

<ul class="global-links"> <li><a title="Nous joindre" href="/fr/coordonnees.html">Nous Joindre</a></li> <li>|</li> <li><a title="Carrières" href="/fr/carrieres.html">Carrières</a></li> <li>|</li> <li><a title="English" href="/en/personal.html">English</a></li> </ul> 

我想要的href是链接上的标题为“英语”的那个。

如果我卡住了,我知道有两种方法可以find相关的文本

  • getElement …(直接parsingDOM)
  • inStr(string操作)

我设法testing他们两个但是:

  • 操纵DOM:我本来以为下面的工作会有效果,但是根本不会,而且会给我一个438的错误。 另外,我不明白是否有可能将href作为标题(因为没有特殊的类或id)

    .getElementsByClassName( “全球通”)。innerText属性

  • 所以我改变了instr方法(操纵位置做一个MID之后…我寻找

    InStr(1,htm.body.innerHTML,“title =”“English”“href =”)

由于双引号,我无法构build我想要以良好方式search的string。 我试图加倍双引号。 我也试过像这样的chr(34)方法

 "title=" & Chr(34) & "English" & Chr(34) & " href=" & Chr(34) 

但是我也不能使它工作,它没有find我的string。

所以我需要帮助来find其标题“英语”的链接的href值,无论是通过DOMsearch或stringsearch。

最后,因为它是一个循环,是否有创build对象/内存使用的最佳做法? 如何处理closures的对象/连接创build一个实例,以释放内存或不超载?

任何帮助将不胜感激。 提前致谢。

编辑

一个例子可以在这里find: https : //www.bnc.ca/fr/particuliers.html

编辑给开始代码

 Sub testAlias() 'title="English" href="https://www.nbc.ca Dim htm As HTMLDocument, table As Object Set htm = New HTMLDocument With CreateObject("MSXML2.XMLHTTP") .Open "GET", "https://www.bnc.ca/fr/particuliers.html", False .send htm.body.innerHTML = .responseText 'Code to continue here... 'Assuming I need to target the <a> with "English" for title and retrieve its href value End With End Sub 

编辑 – 代码与循环不工作

根据David的回答,我在工作表上的一个单元格中创build了一个循环。 我需要附加值和我的域名。

该域名是https://www.bnc.ca ,这里是我testing的值不起作用:

  • /en/particuliers/cartes-de-credit/cartes-de-credit-mastercard/avec-plan-recompenses/allure.html
  • /en/particuliers/cartes-de-credit/cartes-de-credit-mastercard/avec-plan-recompenses/or-ovation.html

我在foreach行有一个错误,指出#91错误(未定义的variables/对象或与块…)

 For Each e In elements(0).ChildNodes 

有人可以帮我解决这个问题吗?

 Sub testAlias() For rid = 2 To 3 'Dim sh As Worksheet 'Set sh = ActiveSheet Dim sh As Worksheet Set sh = ActiveSheet Dim url As String url = "https://www.bnc.ca" & sh.Cells(rid, 1) 'title="English" href="https://www.nbc.ca Dim http As MSXML2.XMLHTTP Dim HTMLDoc As MSHTML.HTMLDocument Dim DOM As Object 'MSXML2.DOMDocument Dim elements As Object Dim ele As Object Dim respText As String Set http = CreateObject("MSXML2.XMLHTTP") Set DOM = CreateObject("MSXML2.DOMDocument") Set HTMLDoc = New MSHTML.HTMLDocument 'for some reason, I can't use CreateObject to do this '## Create the HTTPRequest With http .Open "GET", url, False .send "" '## Load the XML to DOM respText = .responseText End With '## Put in HTML Document HTMLDoc.body.innerHTML = respText Debug.Print respText '## Parse DOM Set elements = HTMLDoc.getElementsByClassName("global-links") 'If elements Is Not Nothing Then '## Assume there is only one class name "global-links" For Each e In elements(0).ChildNodes If e.innerText = "English" Then '## Display the url: sh.Cells(rid, 2).Formula = "" sh.Cells(rid, 2).Formula = e.ChildNodes(0).href End If Next DoEvents 'End If Next rid Application.ScreenUpdating = True End Sub 

你可以使用类似这样的东西来蛮力,否则我可能会尝试使用XPath或更强大的DOMparsing应用程序(需要查看更多的XML结构来协助解决这个问题):

 Sub foo() Dim xmlString As String xmlString = "<ul class=""global-links"">" & _ "<li><a title=""Nous joindre"" href=""/fr/coordonnees.html"">Nous Joindre</a></li>" & _ "<li>|</li>" & _ "<li><a title=""Carrières"" href=""/fr/carrieres.html"">Carrières</a></li>" & _ "<li>|</li>" & _ "<li><a title=""English"" href=""/en/personal.html"">English</a></li>" & _ "</ul>" Dim DOM As Object Set DOM = CreateObject("MSXML2.DOMDOCUMENT") DOM.LoadXML xmlString Dim elements Dim e Set elements = DOM.DocumentElement.GetElementsByTagName("a") For Each e In elements On Error Resume Next If e.ParentNode.ParentNode.XML Like "<ul class=""global-links"">*" Then If e.XML Like "<a title=""English"" href=*" Then MsgBox e.XML End If End If Next End Sub 

更新

我无法使用DOM(尝试加载HTML.responseText时不断出现分析错误,所以我又回到了使用HTMLDocument对象:

 Sub testAlias() 'title="English" href="https://www.nbc.ca Dim HTTP As MSXML2.XMLHTTP Dim HTMLDoc As MSHTML.HTMLDocument Dim DOM As Object 'MSXML2.DOMDocument Dim elements As Object Dim ele As Object Dim respText As String Set HTTP = CreateObject("MSXML2.XMLHTTP") Set DOM = CreateObject("MSXML2.DOMDocument") Set HTMLDoc = New MSHTML.HTMLDocument 'for some reason, I can't use CreateObject to do this '## Create the HTTPRequest With HTTP .Open "GET", "https://www.bnc.ca/fr/particuliers.html", False .send "" '## Load the XML to DOM respText = .responseText End With '## Put in HTML Document HTMLDoc.body.innerHTML = respText '## I tried loading in to DOM but it would not work: 'DOM.LoadXML respText 'If DOM.parseError Then ' MsgBox DOM.parseError.reason ' Stop 'End If '## Parse DOM Set elements = HTMLDoc.getElementsByClassName("global-links") '## Assume there is only one class name "global-links" For Each e In elements(0).ChildNodes If e.innerText = "English" Then '## Display the url: MsgBox e.ChildNodes(0).href End If Next End Sub