Html Agility Pack Xpath 不工作
Html Agility Pack Xpath not working
所以当我尝试做的是使用 Html Agility Pack 解析 HTML 文档。我加载了 html 文档并且它有效。问题出在我尝试使用 XPath 解析它时。我收到 "System.NullReferenceException: 'Object reference not set to an instance of an object.'" 错误。
为了获得我的 xpath,我使用 Chrome 开发 window 并突出显示整个 table 包含我要解析的数据的行,右键单击它并复制 Xpath。
这是我的代码
string url = "https://www.ctbiglist.com/index.asp";
string myPara = "LastName=Smith&FirstName=James&PropertyID=&Submit=Search+Properties";
string htmlResult;
// Get the raw HTML from the website
using (WebClient client = new WebClient())
{
client.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
// Send in the link along with the FirstName, LastName, and Submit POST request
htmlResult = client.UploadString(url, myPara);
//Console.WriteLine(htmlResult);
}
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(htmlResult);
HtmlNodeCollection table = doc.DocumentNode.SelectNodes("//*[@id=\"Table2\"]/tbody/tr[2]/td/table/tbody/tr/td/div[2]/table/tbody/tr[2]/td/table/tbody/tr[2]/td/form/div/table[1]/tbody/tr");
Console.WriteLine(table.Count);
当我 运行 此代码有效时,它会获取 HTML 文档中的所有 table。
var query = from table in doc.DocumentNode.SelectNodes("//table").Cast<HtmlNode>()
from row in table.SelectNodes("//tr").Cast<HtmlNode>()
from cell in row.SelectNodes("//th|td").Cast<HtmlNode>()
select new { Table = table.Id, CellText = cell.InnerText };
foreach (var cell in query)
{
Console.WriteLine("{0}: {1}", cell.Table, cell.CellText);
}
我想要的是一个特定的 table,它包含所有 table 行,其中包含我要解析为对象的数据。
感谢帮助!!!
换行
from table in doc.DocumentNode.SelectNodes("//table").Cast<HtmlNode>()
到
from table in doc.DocumentNode.SelectNodes("//table[@id=\"Table2\"]").Cast<HtmlNode()
这只会 select 具有给定 ID 的特定 table。但是如果你有嵌套的表格,那么你已经相应地改变了你的 xpath 以获得嵌套的 table 行。
所以当我尝试做的是使用 Html Agility Pack 解析 HTML 文档。我加载了 html 文档并且它有效。问题出在我尝试使用 XPath 解析它时。我收到 "System.NullReferenceException: 'Object reference not set to an instance of an object.'" 错误。
为了获得我的 xpath,我使用 Chrome 开发 window 并突出显示整个 table 包含我要解析的数据的行,右键单击它并复制 Xpath。
这是我的代码
string url = "https://www.ctbiglist.com/index.asp";
string myPara = "LastName=Smith&FirstName=James&PropertyID=&Submit=Search+Properties";
string htmlResult;
// Get the raw HTML from the website
using (WebClient client = new WebClient())
{
client.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
// Send in the link along with the FirstName, LastName, and Submit POST request
htmlResult = client.UploadString(url, myPara);
//Console.WriteLine(htmlResult);
}
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(htmlResult);
HtmlNodeCollection table = doc.DocumentNode.SelectNodes("//*[@id=\"Table2\"]/tbody/tr[2]/td/table/tbody/tr/td/div[2]/table/tbody/tr[2]/td/table/tbody/tr[2]/td/form/div/table[1]/tbody/tr");
Console.WriteLine(table.Count);
当我 运行 此代码有效时,它会获取 HTML 文档中的所有 table。
var query = from table in doc.DocumentNode.SelectNodes("//table").Cast<HtmlNode>()
from row in table.SelectNodes("//tr").Cast<HtmlNode>()
from cell in row.SelectNodes("//th|td").Cast<HtmlNode>()
select new { Table = table.Id, CellText = cell.InnerText };
foreach (var cell in query)
{
Console.WriteLine("{0}: {1}", cell.Table, cell.CellText);
}
我想要的是一个特定的 table,它包含所有 table 行,其中包含我要解析为对象的数据。
感谢帮助!!!
换行
from table in doc.DocumentNode.SelectNodes("//table").Cast<HtmlNode>()
到
from table in doc.DocumentNode.SelectNodes("//table[@id=\"Table2\"]").Cast<HtmlNode()
这只会 select 具有给定 ID 的特定 table。但是如果你有嵌套的表格,那么你已经相应地改变了你的 xpath 以获得嵌套的 table 行。