这是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://wantedlink1
到http://wantedlink10
我尝试属性(“href”),但我只得到一个链接
我想列出这样的所有链接:
http://wantedlink1
http://wantedlink2
http://wantedlink3
.
.
.
http://wantedlink10
任何帮助??
答案 0 :(得分:1)
基本上,您可以将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
强>