作者clliu168 (风)
看板C_Sharp
标题[问题] HtmlAgilityPack parsing 问题
时间Sun Feb 7 00:48:54 2010
我想要使用 HtmlAgilityPack 加上 Xpath 来抓取网页资料
我不知道是不是我少了什麽参数,假设我的 html 档案是:
<html>
<head>
<title> XPath Test Page</title>
</head>
<body>
<div class="content">
<p> This is test </p>
</body>
</html>
上面是一个 well-formed 的 html 档案,我可以用 Xpath:
//div[@class='content']/p 顺利的抓到资料
Code 大致上如下:
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.OptionAutoCloseOnEnd = false;
doc.Load(fileName, Encoding.Default); // fileName 档案就是上述 html
// 底下的 xpath 就是 //div[@class='content']/p
HtmlNodeCollection nodes = doc.DocumentNode.SelectNodes(xpath);
可是如果 html 内容少了 </p>
变成 <p> This is test
那 HtmlAgilityPack 就无法抓取到 "This is test" 的内容
HtmlAgilityPack 对於 non well-formed 的 html 是可以读取,但是我需要更
进一步使用 Xpath 抓取资料。
不知道有没有人知道怎麽解决这问题?
我 Google 了很久,都没遇到有人提到这问题,难道是我用法不对嘛?
--
My Blog:
http://webapp-tech.blogspot.com/
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 140.113.0.109