【问题标题】:Html Agility Pack xpath IEnumerableHtml Agility Pack xpath IEnumerable
【发布时间】:2016-03-06 18:26:11
【问题描述】:

我不能添加html代码,因为它非常非常大! 5卷或更多。请点击 htmlWeb.load() 中的链接。 我看了这段代码已经 2 个小时了,我不知道出了什么问题。

HtmlWeb htmlWeb = new HtmlWeb {OverrideEncoding = Encoding.Default};
HtmlAgilityPack.HtmlDocument document = htmlWeb.Load("https://www.parimatch.com/en/sport/futbol/germanija-bundesliga");
var matches = document.DocumentNode.SelectNodes("//tr[@class='bk']").
     Select(tr => new FootballMatch()
     {
         Number = string.Join(" ", tr.SelectNodes("./td[1]//text()[normalize-space()]").Select(t =>t.InnerText)),
         Time = string.Join(" ", tr.SelectNodes("./td[2]//text()[normalize-space()]").Select(t => t.InnerText)),
         Teams  = string.Join(" ", tr.SelectNodes("./td[3]//text()[normalize-space()]").Select(t => t.InnerText)),
         Allowance = string.Join(" ", tr.SelectNodes("./td[4]//text()[normalize-space()]").Select(t => t.InnerText)),
         CoefficientAllowance = string.Join(" ", tr.SelectNodes("./td[5]//text()[normalize-space()]").Select(t => t.InnerText)),
         Total = tr.SelectSingleNode("./td[7]//text()[normalize-space()]").InnerText,
         P1 = tr.SelectSingleNode("./td[10]//text()[normalize-space()]").InnerText,
         X = tr.SelectSingleNode("./td[11]//text()[normalize-space()]").InnerText,
         /*P2 = tr.SelectSingleNode("./td[12]//text()[normalize-space()]").InnerText,
         P1X = tr.SelectSingleNode("./td[13]//text()[normalize-space()]").InnerText,
         P1P2 = tr.SelectSingleNode("./td[14]//text()[normalize-space()]").InnerText,
         P2X = tr.SelectSingleNode("./td[15]//text()[normalize-space()]").InnerText*/
     });

P2,P1X,P1P2,P2X 始终为空。 是否可以将这段代码做得更整洁?

当你点击一个事件时,会出现一个弹出菜单,这个数据也会被读取,但我不需要这个。如何禁用此功能?

【问题讨论】:

  • 不可能!字段是相同的,但有些读取,有些则没有!
  • 你会为自己感到惊讶!字段完全相同,但11后无法读取!

标签: c# asp.net-mvc xpath html-agility-pack


【解决方案1】:

这也不是最漂亮的。但它有效。在对某些细胞进行分离方面仍需要做一些工作。由于某些<td>s 包含<br> 以分隔行。希望这可以帮助您继续前进。

        string xpath = "//tr[@class='bk']";
        HtmlNodeCollection matches = htmlDoc.DocumentNode.SelectNodes(xpath);

        List<List<string>> footballMatches = new List<List<string>>();

        foreach (HtmlNode x in matches)
        {
            List<string> mess = new List<string>();
            HtmlNodeCollection hTC = x.SelectNodes("./td");
            if (hTC.Count > 15)
            {
                for (int i = 0; i < 15; i++)
                {
                    if (i != 5)
                    {
                        mess.Add(hTC[i].InnerText);
                    }
                }
            }
            footballMatches.Add(mess);
        }

【讨论】:

    猜你喜欢
    • 2013-02-13
    • 1970-01-01
    • 2014-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-01
    • 2011-08-24
    • 1970-01-01
    相关资源
    最近更新 更多