我正在尝试编写一个程序,以从您键入的任何网页上的表中抓取数据并将其存储在列表中>。但是,该程序始终崩溃,并显示“必须启用Cookie才能查看此页面。”在我看来,我已经启用了Cookie,因此感到困惑。这是代码:
var web = new HtmlWeb { UseCookies = true };
//Input is a full url
//Ex: "https://sites.google.com/site/chempendix/densities-of-pure-metals"
HtmlAgilityPack.HtmlDocument doc = web.Load(Input);
//The next line will throw the exception:
//'Object reference not set to an instance of an object.'
// HtmlAgilityPack.HtmlNode.SelectSingleNode(...) returned null.
List<List<string>> table = doc.DocumentNode.SelectSingleNode("//table[@class='mydata']")
.Descendants("tr")
.Skip(1)
.Where(tr => tr.Elements("td").Count() > 1)
.Select(tr => tr.Elements("td").Select(td => td.InnerText.Trim()).ToList())
.ToList();
谢谢。