0

我有一个问题 HtmlAgilityPack,我想要下图。但它与线条不重合。我能做些什么?

在此处输入图像描述

在此处输入图像描述

HtmlWeb web = new HtmlWeb();
web.OverrideEncoding = Encoding.GetEncoding("windows-1254");
HtmlAgilityPack.HtmlDocument doc = web.Load("http://www.yerelnet.org.tr/belediyeler/belediye.php?belediyeid=129531");

var nufus = doc.DocumentNode.SelectNodes("/html/body/center/table/tr/td[2]/table/tr[2]/td[2]/table/tr[2]/td/table/tr/td/table[5]/tr/td[2]/table/tr/td[3]/table[2]");

string str = String.Empty;
string line = String.Empty;

if (nufus != null)
{

    for (int i = 0; i < nufus.Count; i++)
    {
        str += nufus[i].InnerText.Replace("&nbsp;", "").Replace("\t", "").Replace("Nüfus Bilgileri", "");

        string[] s = str.Split('\n');

        for (int x = 10; x < s.Count(); x++)
        {
            if (s[x] != String.Empty && s[x] != " ")
            {
                lnufus.Add(s[x].ToString());
                lnufus.Add(s[x].ToString());
                lnufus.Add(s[x].ToString());
                lnufus.Add(s[x].ToString());
            }
        }
    }
}

dataGridView1.DataSource = lnufus.GroupBy(x => x).Select(g => new
{
    Yıl = g.Key, Toplam = g.Key, Kadin = g.Key, Erkek = g.Key
}).ToList();
4

1 回答 1

1

不需要那个奇怪的xpath 查询。这应该工作

var nufus = doc.DocumentNode.SelectNodes("//table[@class='belediyeler']/tr")
            .Skip(1)
            .Select(tr => tr.Elements("td").Select(td => td.InnerText).ToList())
            .Select(td => new { Yil = td[0], Toplam = td[1], Kadin = td[2], Erkek = td[3] })
            .ToList();

dataGridView1.DataSource = nufus;
于 2013-09-14T14:41:45.907 回答