DocumentNode.SelectNodes返回null

时间:2016-08-31 12:52:13

标签: c# xpath html-agility-pack

我想知道你是否可以向我解释我的剧本中有什么问题。我的字符串"温度"总是返回" null"。 我正在使用HtmlAgilityPack。我想做的是从朋友网站获取温度。

我的代码



private void temperatureBtn_Click(object sender, EventArgs e)
        {
            string url = "http://perso.numericable.fr/meteo-kintzheim/";
            HtmlWeb web = new HtmlWeb();
            HtmlAgilityPack.HtmlDocument doc = web.Load(url);

            string temperature = doc.DocumentNode.SelectNodes("/html/body/table[1]/tbody/tr[5]/td[3]/b[2]/font")[0].InnerText;

            MessageBox.Show(temperature.ToString());
        }




如果有人能帮助我,我将非常感激:D

1 个答案:

答案 0 :(得分:0)

http://perso.numericable.fr/meteo-kintzheim/实际上是不同帧的框架集。

将网址更改为http://perso.numericable.fr/meteo-kintzheim/current.html(您想要的框架)并更改您的XPath:

string temperature = doc.DocumentNode.SelectNodes("/html/body/table[1]/tbody/tr[5]/td[3]/b[2]/font")[0].InnerText;

要:

var temperature = doc.DocumentNode.SelectNodes("/html/body/table[1]/tr[5]/td[3]/b[2]/font")[0].InnerText;

省略tbody,因为它不属于该文档。

相关问题