我试图抓住" href"的价值。代码是这样的:
<div class="s_newsbox" style="font-size:12px; vertical-align:middle; overflow: hidden; float:left; margin:10px; margin-bottom:15px; height: 270px; width:280px; border-radius:6px; position:relative; text-align:center; padding:0px">
<div style="background-color:#292929; background-color:rgba(0,0,0,0.8); padding:5px; padding-left:2px; padding-right:10px; width:100%; position:absolute; top:0; left:0;"><b>Samsung nx30 + zoom kit 18/55</b>
</div>
<a href="vendo.php?t=1395911">
<img style="width:100%; height:100%" src="http://img1.juzaphoto.com/shared_files/uploads_mercatino/sell_1395911_small.jpg" alt="">
<br></a>
<div style="line-height:150%; background-color:#292929; background-color:rgba(0,0,0,0.8); padding:5px; position:absolute; bottom:0; left:0; margin-left:auto; width:100%; text-align:left">Venditore:
<a href="me.php?l=it&p=45923"><b>Pierobob</b></a>
<br> Prezzo: <b>350 €</b>
<br> Zona: <b>Bologna</b>
<br>
<a href="vendo.php?t=1395911">Leggi annuncio</a> (8 visite)
<br>
</div>
</div>
我想做的是:
var list = page.DocumentNode.SelectNodes("//div[@class='s_newsbox']");
foreach (var obj in list)
{
var url = obj.SelectSingleNode(".//a").Attributes["href"].Value;
我想抓住价值&#39; vendo.php?t = 1395911&#39;但我获得了另一条线的href值,该线没有父级div与该类&#39; s_newsbox&#39;
我做错了什么?
谢谢!
答案 0 :(得分:0)
只要您不需要s_newsbox div中的任何其他节点,就可以使用更准确的xpath过滤掉相关对象。
var list = page.DocumentNode.SelectNodes("//div[@class='s_newsbox']/a[string-length(@href)>0]");
foreach (var obj in list)
{
var url = obj.SelectSingleNode(".").Attributes["href"].Value;