使用VB.Net中的HtmlAgilityPack检查元素是否具有特定属性

时间:2010-12-25 22:55:58

标签: vb.net html-parsing html-agility-pack

我正在使用HtmlAgilityPack解析HTML。

我想检查某个元素是否具有特定属性。

我想检查<a>标记是否具有href属性。

Dim doc As HtmlDocument = New HtmlDocument()

doc.Load(New StringReader(content))

Dim root As HtmlNode = doc.DocumentNode
Dim anchorTags As New List(Of String)

For Each link As HtmlNode In root.SelectNodes("//a")
    If link.HasAttributes("href") Then doSomething() 'this doesn't work because hasAttributes only checks whether an element has attributes or not
Next

1 个答案:

答案 0 :(得分:4)

像这样:

If link.Attributes("href") IsNot Nothing Then