나는이 코드를 가지고있다.
Dim htmldoc As HtmlDocument = New HtmlDocument()
htmldoc.LoadHtml(strPageContent)
Dim root As HtmlNode = htmldoc.DocumentNode
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
하지만 오류가 발생하고 있습니다 Object reference not set to an instance of an object.
가 Object reference not set to an instance of an object.
문서에 적어도 하나 이상의 앵커 태그가 있습니까? 속성이 종료되는지 어떻게 확인합니까?
나는 이것을 시도했다. if link.HasAttributes("title") then
다른 오류가 발생한다.
Public ReadOnly Property HasAttributes() As Boolean' has no parameters and its return type cannot be indexed.
HtmlAgilityPack이이 XPATH 선택기를 지원하는 경우 //a
를 //a[@href]
For Each link as HtmlNode In root.SelectNodes("//a[@href]")
doSomething()
Next
그렇지 않으면 Attributes
속성을 사용할 수 있습니다.
For Each link as HtmlNode In root.SelectNodes("//a")
If link.Attributes.Any(Function(a) a.Name = "href") Then doSomething()
Next
Dim htmldoc As HtmlDocument = New HtmlDocument()
htmldoc.LoadHtml(strPageContent)
Dim root As HtmlNode = htmldoc.DocumentNode
var nodes = root.SelectNodes("//a[@href and @title]")
if (nodes <> Null) Then
For Each link As HtmlNode In nodes
If link.HasAttributes("href") Then doSomething() 'this doesn't work because hasAttributes only checks whether an element has attributes or not
Next
end if
또한 속성을 확인할 수 있습니다 : link.Attributes [ "title"] null 인 경우 속성이 없습니다. 같은 링크. 애트리뷰트 [ "href"] 등.
속성 링크 .HasAttributes는 해당 태그에만 속성이 있음을 보여 주며, bool 값입니다.