0

我是 C# XAML 和 Linq 初学者,我需要使用 linq 解析此 html 代码的帮助。我知道如何使用 linq 获取 strong> 标签之间的值,但我需要提取 td class= 'sivo' 之间的值并获取电影的英文名称,并获取其链接和在 https 链接之后显示的相应播放时间。

<div class='movieItemWrapper'>
       <table cellspacing='2' cellpadding='0'>
           <tr>
             <td>
                  <strong>Riddick</strong>
             </td>
           </tr>
           <tr>
               <td class='sivo'>Riddick</td>
           </tr>
              <tr>
                 <td class='sivo'>DV. 11  -
                    <a href='https://some.text.here/performance.aspx?oid=C9A53000023KBHTNGF&amp;coid=10000000014OCPXCOG' target='_blank'>16.50</a>, DV. 11  - <a href='https://some.text.here/performance.aspx?oid=20B53000023KBHTNGF&amp;coid=10000000014OCPXCOG' target='_blank'>19.15</a>, DV. 7  - <a href='https://some.text.here/performance.aspx?oid=41B53000023KBHTNGF&amp;coid=10000000014OCPXCOG' target='_blank'>20.00</a>, DV. 11  - <a href='https://some.text.here/performance.aspx?oid=05B53000023KBHTNGF&amp;coid=10000000014OCPXCOG' target='_blank'>21.40</a>, DV. 7  - <a href='https://some.text.here/performance.aspx?oid=86B53000023KBHTNGF&amp;coid=10000000014OCPXCOG' target='_blank'>22.30</a>,
                </td>
              </tr>
           </table>
        </div>
        <div class='movieItemWrapper'>
           <table cellspacing='2' cellpadding='0'>
              <tr>
                 <td>
                    <strong>Domestic name</strong>
                 </td>
              </tr>
              <tr>
                 <td class='sivo'>The Conjuring</td>
             </tr>
             <tr>
                 <td class='sivo'>DV. 3  - <a href='https://some.text.here/performance.aspx?oid=2AA53000023KBHTNGF&amp;coid=10000000014OCPXCOG' target='_blank'>17.00</a>, DV. 2  - <a href='some.text.here/performance.aspx?oid=4EA53000023KBHTNGF&amp;coid=10000000014OCPXCOG' target='_blank'>19.00</a>, DV. 2  - <a href='https://some.text.here.hr/performance.aspx?oid=E3B53000023KBHTNGF&amp;coid=10000000014OCPXCOG' target='_blank'>21.20</a>,
                 </td>
             </tr>
      </table>
</div>

我试过这段代码

var times = from tr in doc.DocumentNode.Descendants("tr")
            from td in tr.Descendants("td").Where(x => x.Attributes["class"].Value == "sivo")
            select td;
4

1 回答 1

0
HtmlAgilityPack.HtmlDocument doc= new HtmlAgilityPack.HtmlDocument(); htmlDoc.LoadHtml(stringWithHtml);        
  var names = doc.DocumentNode.Descendants().Where(n => n.Name == "td").Where(x => x.Attributes["class"] != null && x.Attributes["class"].Value == "sivo");

    var links = doc.DocumentNode.Descendants().Where(n => n.Name == "td").Where(x => x.Attributes["class"] != null && x.Attributes["class"].Value == "sivo").Select(x => x.Descendants().Where(s => s.Name == "a"));
    foreach (var item in names)
    {
        var blabla = item.InnerText;
    }
    foreach (var item in links)
    {
        var lnks = item.Select(x => x.Attributes["href"].Value);//links
        var times = item.Select(x => x.InnerText);  //times
    }
于 2013-09-13T20:15:00.370 回答