使用htmlAgilitypack进行C#html解析 [英] C#html parsing using htmlAgilitypack
本文介绍了使用htmlAgilitypack进行C#html解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在C#中有以下代码,但它无效。我是整个C#的新手并解析它,但我需要让它工作。任何人都知道我为什么得到:对象引用未设置为对象的实例。
I have the following code in C# but it is not working. I am new to the whole C# and parsing thing but I need to get this working. Anyone knows why i am getting:"Object reference not set to an instance of an object."
using System.Text;
using HtmlAgilityPack;
namespace HtmlParser
{
class Program
{
static void Main(string[] args)
{
// The HtmlWeb class is a utility class to get the HTML over HTTP
HtmlWeb htmlWeb = new HtmlWeb();
// Creates an HtmlDocument object from an URL
HtmlAgilityPack.HtmlDocument document = htmlWeb.Load("http://allrecipes.com/search/default.aspx?w0=chicken&w1=beef");
// Targets a specific node
HtmlNode content_wrapper = document.GetElementbyId("content-wrapper");
System.Console.WriteLine(content_wrapper.ToString());
HtmlNodeCollection search_results = content_wrapper.SelectNodes("//div[@class='searchResult']");
foreach (HtmlNode result in search_results)
{
string recipe_name = result.SelectSingleNode("*[@class='resultTitle']").InnerText;// error appears here
System.Console.WriteLine(recipe_name);
}
System.Console.ReadKey();
}
}
}
已添加代码块[/ Edit]
Code block added[/Edit]
推荐答案
我认为您的 content_wrapper $ c $中的搜索结果没有HTML元素 class
c> element。
I think there''s no HTML-element with your search result asclass
in yourcontent_wrapper
element.
这篇关于使用htmlAgilitypack进行C#html解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文