htmlagilitypack chooseinglenode返回null

时间:2017-12-03 20:16:01

标签: c# html html-agility-pack selectsinglenode

问题是,它在20次尝试中只运行一次,而且完全是随机的。无法处理这种情况......帮助将不胜感激^^

        WebClient client = new WebClient();

        client.Encoding = Encoding.UTF8;
        try
        {
            html = client.DownloadString(url);
        }
        catch (WebException)
        {
            if (MessageBox.Show("url'de hata", "Hata", MessageBoxButtons.OK, MessageBoxIcon.Error) == DialogResult.OK)
            {

            }

        }

        HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
        HtmlWeb web = new HtmlWeb();

        doc.LoadHtml(html);

        HtmlNode foo = doc.DocumentNode.SelectSingleNode(Xpath);    

        Listbox1.Items.Add(foo.Attributes["href"].Value); //error here 
        currentPage = (foo.Attributes["href"].Value);

1 个答案:

答案 0 :(得分:0)

您必须检查节点是否为空。服务器可能会为每个请求返回不同的响应服务器可能会返回您想要的20到1个元素。你确定内容是一样的吗?会议可能因情况而异。确保成功和不成功的内容相同。

相关问题