vb.net使用htmlagilitypack獲取所有屬性值

attributes href html-agility-pack vb.net

這是html

<div id="catlist-listview" class="cat-listview cat-listbsize">  
 <ul>
  <li><a href="http://wantedlink1" rel="bookmark" title="sometitel1" class="sonra">title1</a></li>    
  <li><a href="http://wantedlink2" rel="bookmark" title="sometitel2" class="sonra">title2</a></li>
  <li><a href="http://wantedlink3" rel="bookmark" title="sometitel3" class="sonra">title3</a></li>
  <li><a href="http://wantedlink4" rel="bookmark" title="sometitel4" class="sonra">title4</a></li>
  <li><a href="http://wantedlink5" rel="bookmark" title="sometitel5" class="sonra">title5</a></li>
  <li><a href="http://wantedlink6" rel="bookmark" title="sometitel6" class="sonra">title6</a></li>
  <li><a href="http://wantedlink7" rel="bookmark" title="sometitel7" class="sonra">title7</a></li>
  <li><a href="http://wantedlink8" rel="bookmark" title="sometitel8" class="sonra">title8</a></li>
  <li><a href="http://wantedlink9" rel="bookmark" title="sometitel9" class="sonra">title9</a></li>
  <li><a href="http://wantedlink10 " rel="bookmark" title="sometitel10" class="sonra">title10</a></li>
 </ul>
</div>

我的代碼是

dim htmldoc as new htmldocument
htmldoc.loadhtml(source)
for each link as htmlnode in htmldoc.document.selectnodes("//*[@id='catlist-listview']/ul")
textbox3.text = link.innerhtml
next

輸出是

      <li><a href="http://wantedlink1" rel="bookmark" title="sometitel1" class="sonra">title1</a></li>    
      <li><a href="http://wantedlink2" rel="bookmark" title="sometitel2" class="sonra">title2</a></li>
      <li><a href="http://wantedlink3" rel="bookmark" title="sometitel3" class="sonra">title3</a></li>
      <li><a href="http://wantedlink4" rel="bookmark" title="sometitel4" class="sonra">title4</a></li>
      <li><a href="http://wantedlink5" rel="bookmark" title="sometitel5" class="sonra">title5</a></li>
      <li><a href="http://wantedlink6" rel="bookmark" title="sometitel6" class="sonra">title6</a></li>
      <li><a href="http://wantedlink7" rel="bookmark" title="sometitel7" class="sonra">title7</a></li>
      <li><a href="http://wantedlink8" rel="bookmark" title="sometitel8" class="sonra">title8</a></li>
      <li><a href="http://wantedlink9" rel="bookmark" title="sometitel9" class="sonra">title9</a></li>
      <li><a href="http://wantedlink10 " rel="bookmark" title="sometitel10" class="sonra">title10</a></li>

我想得到所有,只有http://wantedlink1http://wantedlink10我嘗試屬性(“href”),但我只得到一個鏈接,我想列出所有這樣的鏈接:

http://wantedlink1 
http://wantedlink2 
http://wantedlink3 
.
. 
. 
http://wantedlink10

任何幫助?

一般承認的答案

基本上,您可以更改SelectNodes() XPath以選擇單個<a>元素而不是<ul> 。然後從這一點開始,迭代結果並逐個獲得href屬性將很容易。或者使用LINQ實現相同的功能,例如:

'select <a> elements'
Dim links = htmldoc.Document.SelectNodes("//*[@id='catlist-listview']/ul/li/a")
'project to IEnumerable of href attribute value'
Dim hrefs = links.Cast(Of HtmlNode)().Select(Function(x) x.GetAttributeValue("href", ""))
'join the `hrefs`, separated by newline, into one string'
textbox3.text = String.Join(Environment.NewLine, hrefs)

dotnetfiddle demo



許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
這個KB合法嗎? 是的,了解原因
許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
這個KB合法嗎? 是的,了解原因