【问题标题】:Max Value with Substring with HTML Agility Pack带有 HTML 敏捷包的子字符串的最大值
【发布时间】:2014-01-01 12:54:33
【问题描述】:

我似乎无法通过这段代码让这个 xpath 查询与 HTMLAgilityPack 一起工作,我想知道是否有人有任何建议。

这是我目前的查询,但我似乎无法让它返回一个数字。

DocumentNode.GetAttributeValue("max(a[(@class='shackmsg')]/@href/substring-after(.,?id='))", "");

我试图在= 登录所有hrefs 后,在href 属性中获取MAX 值,并使用shackmsg 类.

How long is the beta live before it goes retail? No one knows. We do know t</span> : </a><span class="oneline_user ">legsbrogan</span>
</div>
</li>
<li id="item_31218936" class="">
<div class="oneline oneline3 op olmod_ontopic olauthor_189801">
<a class="shackmsg" rel="nofollow" href="?id=31218936" onclick="return clickItem( 31218933, 31218936);"><span class="oneline_body"><b><u><span class="jt_yellow">Current Multiplayer Servers</span>!</u></b>
<span class="jt_sample"><span class="jt_green">Nighteyes's Japan Server: </span> <span class="jt_lime">(PvE)</span>: <b>211.15.2.34</b></span>
<span class="jt_sample"><span class="jt_green">zolointo's Canada Server: </span> <span class="jt_lime">(</span></span></span> : </a><span class="oneline_user ">legsbrogan</span>
</div>
</li>
<li id="item_31218938" class="last">
<div class="oneline oneline2 op olmod_ontopic olauthor_189801">
<div class="treecollapse">
  <a class="open" rel="nofollow" href="#" onclick="toggle_collapse(31218938); return false;" title="Toggle">toggle</a>
</div>
<a class="shackmsg" rel="nofollow" href="?id=31218938" onclick="return clickItem( 31218933, 31218938);"><span class="oneline_body">Had fun freezing my ass off last night with a bunch of shackers.  Not sure who started the big tower we f...</span> : </a><span class="oneline_user ">legsbrogan</span>
</div>
<ul>
<li id="item_31218966" class="">
<div class="oneline oneline1 olmod_ontopic olauthor_128401">
<a class="shackmsg" rel="nofollow" href="?id=31218966" onclick="return clickItem( 31218933, 31218966);"><span class="oneline_body">wasn't me. I hung out on my ship for a bit listening to your kid play Christmas songs for a bit and then ...</span> : </a><span class="oneline_user ">jonin</span><a class="lightningbolt" rel=\"nofollow\" href="http://www.shacknews.com/user/jonin/posts?result_sort=postdate_asc"><img src="http://cf.shacknews.com/images/bolt.gif" alt="This person is cool!" /></a>
</div>
</li>
<li id="item_31219008" class="last">
<div class="oneline oneline0 olmod_ontopic olauthor_8618">
<a class="shackmsg" rel="nofollow" href="?id=31219008" onclick="return clickItem( 31218933, 31219008);"><span class="oneline_body">haha i heard you guys booby trapped some poor sap's space ship</span> : </a><span class="oneline_user ">Break</span><a class="lightningbolt" rel=\"nofollow\" href="http://www.shacknews.com/user/Break/posts?result_sort=postdate_asc"><img src="http://cf.shacknews.com/images/bolt.gif" alt="This person is cool!" /></a>
</div>
</li>
</ul>

有什么建议吗?

【问题讨论】:

  • 您观察到的错误/意外行为是什么?
  • 它正在返回 "" 值,因为它似乎找不到匹配项。
  • GetAttributeValue 尝试获取所选节点的 sinhe 属性的值。你试过SelectSingleNode然后得到价值了吗?

标签: c# xpath html-parsing html-agility-pack xpath-2.0


【解决方案1】:

据我所知有两个问题:

  • 您只是在当前上下文中扫描锚标记。您可能希望扩展到到处扫描(在查询开头使用//):

    //a[@class='shackmsg']/@href/substring-after(., '?id=')
    

    请注意,我删除了一对不必要的括号。

  • 如果我没有完全弄错的话,HTML Agility Pack 只支持 XPath 1.0(但我不完全确定)。虽然System.Xml.XPath 说它实现了 XPath 2.0 数据模型,但它实际上并没有实现 XPath 2.0(可能这样做是为了让第三方 API 可以实现这个 API 并同时提供 XPath 2.0/XQuery 支持)。也可以看看this discussion on .NET's XPath 2.0 support

缺少 XPath 2.0 支持将表现为两个问题:

  1. 函数substring-after(...) 不存在。

    解决您的问题的方法是使用string-lenght($string)substring($string, $start, $length) 提取最后n 位数字,或使用translate(...) 删除一些字符:

    translate('?id=31219008', '?id=', '')
    

    将删除字符类[?id=] 中的所有出现(但它没有,我只想突出显示它不匹配字符串,而是该集合的单个字符!)。

  2. 您不能在轴步中应用函数。这意味着,您无法找到子字符串的最大值。

    可能的解决方案:仅获取所有子字符串并从 XPath 外部找到最大值。

【讨论】:

    【解决方案2】:

    您可以将 XPath 与 HTML Agility Pack 结合使用并编写以下代码:

    var value = doc.DocumentNode.SelectNodes("//a[@class='shackmsg']").Select(
                       x => x.Attributes["href"].Value.Substring(4)).Max();
    
    Console.WriteLine(value);
    

    这个输出:

    31219008
    

    在此代码中,我假设始终存在 href 属性并且始终具有以下结构:

    "?id=XXXX"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-28
      • 1970-01-01
      • 2014-03-07
      • 2011-01-26
      相关资源
      最近更新 更多