HTML Agility Pack을 사용하여 특정 테이블을 가져 오는 데 문제가 있습니다. 실제 HTML도 변경할 수 없으므로 다른 ID 또는 클래스 또는 다른 것을 사용할 수 없습니다.
다른 사람이 다음 각 테이블에 액세스하는 방법을 보여줄 수 있습니까?
<table class="newTable">
//table 1 contents
<table border="0" cellpadding="3" cellspacing="2" width="100%">
//table 1 - A contents
</table>
</table>
<table border="0" cellpadding="0" cellspacing="0" class="newTable">
//table 2 contents
<table width="100%" border="0" cellspacing="2" cellpadding="0">
//table 2 - A contents
</table>
<table width="100%" border="0" cellspacing="2" cellpadding="0">
//table 2 - B contents
</table>
<table width="100%" cellspacing="2" cellpadding="0">
//table 2 - C contents
</table>
</table>
<table>
//table 3 contents
</table>
지금 내가 다음을 부르면
HtmlNode table = doc.DocumentNode.SelectSingleNode("//table");
foreach (var cell in table.SelectNodes("//tr/td"))
{
string someVariable = cell.InnerText
}
나는 모든 것을 통과 할 것이다. 테이블을 다르게 액세스하여 데이터를 저장할 위치를 상관시킬 수 있기를 원합니다.
나는 뭔가를 보려고 노력했다.
doc.DocumentNode.SelectNodes("//table[1]");
하지만 인덱스를 사용하면 테이블을 지정하려고 할 때 작동하지 않는 것처럼 보이지만 모든 테이블 또는 아무 것도 읽지 않습니다.
같은 일이 이것에 적용됩니다, 그것은 전혀 작동하지 않거나 모든 것을 얻습니다.
foreach (var cell in table.SelectNodes("//table").Skip(some_number))
{
string someVariable = cell.InnerText
}
HTML 애자일 팩 1.4.9의 NuGet 패키지를 사용하고 있습니다.
편집하다:
표 1 - A의 내용 만 얻으려는 시도. 둘 다 null 또는 endcodingfound 예외를 제공합니다.
HtmlNode table = doc.DocumentNode.SelectSingleNode("//table/tr/td/table[1]");
HtmlNode table = doc.DocumentNode.SelectSingleNode("//table[1]/tr/td/table[1]");
두 번째 호출에서 오류가 발생하면 "// tr / td"가 루트 요소로 돌아갑니다. 인덱서는 문제의 첫 번째 부분에 대한 올바른 솔루션이고 두 번째는 현재 위치에서 탐색하도록 지정하여 수정할 수 있습니다.
HtmlNode table = doc.DocumentNode.SelectSingleNode("//table[1]");
foreach (var cell in table.SelectNodes(".//tr/td")) // **notice the .**
{
string someVariable = cell.InnerText
}
어떤 일이 일어나고 있는지 잘 모르지만 테스트 테이블을이 코드 로 확장 하면 다음 테스트가 내 테스트에서만 작동합니다. 좀 더 많은 맥락을 공유 할 필요가 있음을 의미 할 수도 있습니다.
이 테스트에 사용한 문서는 다음과 같습니다.
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<table class="newTable">
<tr>
<td>
<table border="0" cellpadding="3" cellspacing="2" width="100%">
<tr><td>
//table 1 - A contents
</td></tr>
</table>
</td>
</tr>
</table>
<table border="0" cellpadding="0" cellspacing="0" class="newTable">
<tr>
<td>
//table 2 contents
<table width="100%" border="0" cellspacing="2" cellpadding="0">
<tr>
<td>
//table 2 - A contents
</td>
</tr>
</table>
<table width="100%" border="0" cellspacing="2" cellpadding="0">
<tr>
<td>
//table 2 - B contents
</td>
</tr>
</table>
<table width="100%" cellspacing="2" cellpadding="0">
<tr>
<td>
//table 2 - C contents
</td>
</tr>
</table>
</td>
</tr>
</table>
<table>
<tr>
<td>
//table 3 contents
</td>
</tr>
</table>
</body>
</html>
그리고이 값을 추출하는 코드는 다음과 같습니다.
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(text);
var node1A = doc.DocumentNode.SelectSingleNode("//table[1]//table[1]");
string content1A = node1A.InnerText;
Console.WriteLine(content1A);
var node2C = doc.DocumentNode.SelectSingleNode("//table[2]//table[3]");
string content2C = node2C.InnerText;
Console.WriteLine(content2C);
쇼 :
좋아, 나는 실제 HTML을 가져 왔고 NullReference도 얻는다. Agility Pack을 크게 혼란스럽게하는 이유가있을 수 있습니다. Linq API를 사용한 몇 가지 실험이 효과가있는 것 같지만, 이것이 당신을 대신 할 수 있기를 희망합니다.
var table = doc.DocumentNode.DescendantsAndSelf("table").Skip(1).First().Descendants("table").First();
var tds = table.Descendants("td");