【问题标题】:NullReferenceException in HtmlAgilityPackHtmlAgilityPack 中的 NullReferenceException
【发布时间】:2012-04-14 07:50:11
【问题描述】:

我正在尝试使用xpath 从下面提到的网址中提取link

string url = "http://www.album-cover-art.org/search.php?q=Ruin+-+Live+Album+Version+Lamb+of+God"

我的代码:

HtmlAgilityPack.HtmlWeb web = new HtmlAgilityPack.HtmlWeb();
HtmlAgilityPack.HtmlDocument htmlDoc = new HtmlAgilityPack.HtmlDocument();
htmlDoc = web.Load(url); //Exception generated here Line 23

if (htmlDoc.DocumentNode != null)
{
  HtmlNode linkNode = htmlDoc.DocumentNode.SelectSingleNode(".//*[@id='related_search_row']/img/@src");
  if (linkNode != null)
        Console.WriteLine(linkNode.InnerText);
}

上面的代码编译得很好,但是当我尝试运行它时会产生异常

Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object.

完整的堆栈跟踪

System.NullReferenceException: Object reference not set to an instance of an object.
   at HtmlAgilityPack.HtmlDocument.ReadDocumentEncoding(HtmlNode node) in C:\Source\htmlagilitypack\Trunk\HtmlAgilityPack\HtmlDocument.cs:line 1916
   at HtmlAgilityPack.HtmlDocument.PushNodeEnd(Int32 index, Boolean close) in C:\Source\htmlagilitypack\Trunk\HtmlAgilityPack\HtmlDocument.cs:line 1805
   at HtmlAgilityPack.HtmlDocument.Parse() in C:\Source\htmlagilitypack\Trunk\HtmlAgilityPack\HtmlDocument.cs:line 1468
   at HtmlAgilityPack.HtmlDocument.Load(TextReader reader) in C:\Source\htmlagilitypack\Trunk\HtmlAgilityPack\HtmlDocument.cs:line 769
   at HtmlAgilityPack.HtmlWeb.Get(Uri uri, String method, String path, HtmlDocument doc, IWebProxy proxy, ICredentials creds) in C:\Source\htmlagilitypack\Trunk\HtmlAgilityPack\HtmlWeb.cs:line 1515
   at HtmlAgilityPack.HtmlWeb.LoadUrl(Uri uri, String method, WebProxy proxy, NetworkCredential creds) in C:\Source\htmlagilitypack\Trunk\HtmlAgilityPack\HtmlWeb.cs:line 1563
   at HtmlAgilityPack.HtmlWeb.Load(String url, String method) in C:\Source\htmlagilitypack\Trunk\HtmlAgilityPack\HtmlWeb.cs:line 1149
   at HtmlAgilityPack.HtmlWeb.Load(String url) in C:\Source\htmlagilitypack\Trunk\HtmlAgilityPack\HtmlWeb.cs:line 1107
   at ScreenScrapping.Program.Main(String[] args) in c:\Users\ranveer\csharp\ScreenScrapping\ScreenScrapping\Program.cs:line 23

所以,我的问题是为什么会出现这个异常。

【问题讨论】:

  • 使用 HtmlAgilityPack 的 1.4.3 版本,您的示例工作正常。你用的是哪个版本?
  • @nemesv:现在我正在使用 HtmlAgilityPack ver1.4.3。现在我没有收到任何错误,但Console.WriteLine(linkNode.InnerText); 没有提供任何输出,linkNode 也不是 null 我检查了。
  • 当我使用 xpath //title/text() 时它工作正常,但是当我切换到涉及使用 /@href or /@src 访问属性的 xpath 表达式时它不起作用。

标签: c# html-agility-pack


【解决方案1】:

这是 HtmlAgilityPack 中的一个错误。您尝试解析的文档有 <meta http-equiv="Content-Type" content="text/html; charset=iso-utf-8">,其中 charset 值 (iso-utf-8) 无法被 AgilityPack 解析为有效的编码名称。正如 Simon Mourier said这是 1.4.0.0 中引入的一个错误

为避免这种情况,请手动从流中加载文档并手动设置编码,如下所示:

var htmlDoc = new HtmlDocument();
htmlDoc.OptionReadEncoding = false;
var request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "GET";
using (var response = (HttpWebResponse)request.GetResponse())
{
    using (var stream = response.GetResponseStream())
    {
        htmlDoc.Load(stream, Encoding.UTF8);
    }
}

【讨论】:

  • 感谢您的回答。它工作得很好。现在我正在使用 HtmlAgilityPack ver1.4.3。现在我没有收到任何错误,但Console.WriteLine(linkNode.InnerText); 没有提供任何输出,linkNode 也不是 null 我检查了。
  • 当我使用 xpath //title/text() 时它工作正常,但是当我切换到涉及使用 /@href or /@src 访问属性的 xpath 表达式时它不起作用。
  • @Noob,尝试像“//a[@href]”一样使用它——属性使用方括号
  • 我尝试了.//*[@id='related_search_row'][1]/img/[@src],但它正在生成错误Unhandled Exception: System.Xml.XPath.XPathException: Expression must evaluate to a node-set.
  • 我发现使用 xpath .//*[@id='related_search_row'][1]/img 比使用 string val = linkNode.Attributes["src"].Value 的解决方法。我想知道这是唯一的方法。
猜你喜欢
  • 2019-06-02
  • 1970-01-01
  • 1970-01-01
  • 2010-11-05
  • 2011-07-24
  • 2013-03-12
  • 2016-09-20
  • 2013-06-12
  • 1970-01-01
相关资源
最近更新 更多