Dot-Net

您如何在 vb.net 中解析 HTML

  • February 5, 2009

我想知道是否有一種簡單的方法可以在 vb.net 中解析 HTML。我知道 HTML 不是 XML 的嚴格子集,但如果可以這樣對待它會很好。有什麼東西可以讓我在 VB.net 中以類似 XML 的方式解析 HTML 嗎?

我喜歡Html Agility 包——它對開發人員非常友好、免費且原始碼可用。

‘也添加 prog ref: Microsoft.mshtml

‘然後在頁面上:

Imports mshtml

Function parseMyHtml(ByVal htmlToParse$) As String
   Dim htmlDocument As IHTMLDocument2 = New HTMLDocumentClass()
   htmlDocument.write(htmlToParse)
   htmlDocument.close()

   Dim allElements As IHTMLElementCollection = htmlDocument.body.all

   Dim allInputs As IHTMLElementCollection = allElements.tags("a")
   Dim element As IHTMLElement
   For Each element In allInputs
       element.title = element.innerText
   Next

   Return htmlDocument.body.innerHTML
End Function

如發現here

引用自:https://stackoverflow.com/questions/516811