vb.net obtenir tous les attributs en utilisant htmlagilitypack

attributes href html-agility-pack vb.net

Question

c'est le 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>

et mon code est

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

la sortie est

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

Je veux obtenir tous et seulement http://wantedlink1 à http://wantedlink10 j'essaie d'attributs ("href") mais je ne reçois qu'un seul lien je veux lister tout le lien comme ceci:

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

de l'aide ??

Réponse acceptée

En gros, vous pouvez changer XPath pour que SelectNodes() sélectionne des <a> éléments individuels au lieu de <ul> . Ensuite, à partir de ce point, il sera facile de parcourir le résultat et d’obtenir l’attribut href un par un. Ou vous obtenez la même chose avec LINQ, comme par exemple:

'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




Sous licence: CC-BY-SA with attribution
Non affilié à Stack Overflow
Est-ce KB légal? Oui, apprenez pourquoi
Sous licence: CC-BY-SA with attribution
Non affilié à Stack Overflow
Est-ce KB légal? Oui, apprenez pourquoi