问题是,它仅在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);
您必须检查节点不是null。服务器可以返回每个请求的不同响应。服务器可能会返回您想要的20到1个元素。您确定内容是否相同?会议可能会因情况而异。确保成功和失败的内容相等。