【发布时间】:2020-11-24 20:37:51
【问题描述】:
我很想使用 HtmlAgilityPack 为某个嵌套的 div 类抓取网页,该类包含带有我要提取的数据的 span 标签
我想要的元素文本的完整 XPath:
/html/body/div[2]/div/div[1]/div/table/tbody/tr/td/span
我的代码:
static void Main(string[] args)
{
HtmlAgilityPack.HtmlWeb web = new HtmlAgilityPack.HtmlWeb();
HtmlAgilityPack.HtmlDocument doc = web.Load("http://watchout4snakes.com/wo4snakes/Random/RandomParagraph");
var paragraph = doc.DocumentNode.SelectNodes("//div[@class='mainBody']//div[@class='content']//div[@class='resultContainer']" +
"//div[@class='resultBox']//table[@class='paragraphResult']").ToList();
foreach (var item in paragraph)
{
Console.WriteLine(item.InnerText);
}
}
我尝试将完整的 XPath 放入 doc.DocumentNode.SelectNodes() 以及只是 Xpath //*[@id='result']
我的问题是它要么不返回任何内容,要么在doc.DocumentNode.SelectNodes() 行上收到错误消息Unhandled exception. System.ArgumentNullException: Value cannot be null. (Parameter 'source')。
【问题讨论】:
标签: c# html web-scraping html-agility-pack