【问题标题】:Check if element has a specific attribute using HtmlAgilityPack in VB.Net在 VB.Net 中使用 HtmlAgilityPack 检查元素是否具有特定属性
【发布时间】:2011-05-30 17:49:21
【问题描述】:

我正在使用 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

【问题讨论】:

  • 我在我的程序中使用完全相同的代码,并且在 foreach 行中出现错误Object reference not set to an instance of an object.
  • @Smith:不要使用我的代码,而是使用 Slaks 的固定代码

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


【解决方案1】:

像这样:

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

【讨论】:

猜你喜欢
  • 2019-04-30
  • 2022-06-25
  • 2020-07-09
  • 2018-02-09
  • 1970-01-01
  • 2022-12-06
  • 1970-01-01
  • 1970-01-01
  • 2014-11-30
相关资源
最近更新 更多