vb.net get all attributes value using htmlagilitypack

attributes href html-agility-pack vb.net

Question

Here is the 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>

the code I use is

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

the result is

      <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>

I want to get everything.http://wantedlink1 to http://wantedlink10 When I use attributes("href"), I only receive one link; I want a list of all links.

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

any help??

1
2
2/25/2016 11:29:00 PM

Accepted Answer

In essence, you may modify XPath forSelectNodes() to be choosing people<a> elements as opposed to<ul> . Following this, it will be simple to cycle over the outcome and gethref attribute by attribute. Or you may use LINQ to do the same thing, like in the following example:

'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

1
2/26/2016 8:53:48 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow