2012-03-02 2 views
1

HTML 애자일 팩과 함께이 HTML 페이지를 구문 분석하려고하지만 예상대로 작동하지 않습니다. 해당 테이블의 <tr>....</tr>HTML 민첩성 팩이 예상대로 작동하지 않습니다.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" lang="de-ch" xml:lang="de-ch"> 
<head> 
</head> 
<body id="Adressservices"> 
    <div id="page"> 
     <div id="page-544"> 
      <table class="full"> 
       <thead> 
        <tr> 
         <th class="first" scope="col" style="width: 18%;">Type</th> 
         <th class="col" style="width: 20%;">Name</th> 
         <th class="col">Date</th> 
         <th class="col" style="text-align: right; width: 10%;">Size</th> 
        </tr> 
       </thead> 
       <tbody> 
        <tr> 
         <td class="first">Change</td> 
         <td><a href="/download?file=5210044">somefile01.zip</a></td> 
         <td style="width: 5%;"><b class="filesize">2012-03-01</b></td> 
         <td style="text-align: right;"><b class="filesize">881.00</b></td> 
        </tr> 
        <tr> 
         <td class="first">Change</td> 
         <td><a href="/download?file=7610042">somefile02.zip</a></td> 
         <td style="width: 5%;"><b class="filesize">2012-02-01</b></td> 
         <td style="text-align: right;"><b class="filesize">1400.00</b></td> 
        </tr> 
        <tr>.....</tr> 
       </tbody> 
      </table> 
     </div> 
    </div> 
</body> 
</html> 

실제 페이지가 꽤 몇 행 :

은 (단축) 내 페이지입니다.

나는 HTML 민첩성 팩이 코드를 사용하여와 잘 페이지 다운로드 할 수 있었다 :
HtmlWeb web = new HtmlWeb(); 
HtmlDocument archiveDoc = web.Load(_archiveUrl); 
var tables = archiveDoc.DocumentNode.SelectNodes("//table"); 

그래서 내 <table> 요소에 핸들을 가져올를, 잘 작동합니다.

는 지금은 그 테이블 내에서 <tr> 요소를 얻으려고 노력했다, 나는이 시도 : 여기

HtmlNode node = tables[0]; 
var allTRNodes = node.SelectNodes("tbody/tr"); 
var firstTR = allTRNodes[0]; 

를, 내가 기대 <tr> 노드와 N을받지 못했습니다 -하지만 두 . 그리고 그 첫 번째는 중 ...

가 그럼 난 Linq에 대 "HTML"을 시도 <td> 유형의 Y 자식 노드의 목록이 포함되어 있지 않습니다

HtmlNode node = tables[0]; 
var firstTR = node.Element("tbody").Element("tr"); 

그러나 다시를 : 난 유형 <td>의 Y 자식 노드의 목록을 포함하는 첫 번째 <tr> 노드를받지 중 ... 예상대로도 꽤 작동하지 않았다 처음 <tr> 내부의 모든 <td> 노드 목록을 얻으려고 노력

:

노드 대신, <td> 노드가 예상됩니다. 단지 세 개의 자식 노드가 있습니다. #text 중 두 개, 마지막 노드는 <td>입니다. 왜 그렇습니까?

어떤 아이디어 ...... 중첩 노드로 <td> 노드 목록을 잘못 해석 HTML 민첩성 팩처럼 보인다? 생각? 이것을 해결하는 방법을 알려주시겠습니까? 이 예에서와 같이

+1

그것은 이상한 샘플을 채택합니다. HTML 예제에서'allTRNodes'는 3 개의'tr' 노드를 가지고 있습니다 (마지막 노드는 ...); 'allTDNodes'에는 8 개의'td' 노드가 있습니다 (HtmlAgilityPack v2.0.50727). 한 가지 더 가정합니다. 예를 들어 포함시키지 않은 HTML 코드가 깨졌습니다. – Alex

+0

HAP의 최신 안정적인 다운로드와 함께 여기 있습니다. – Oded

+0

@Alex : 네,'allTDNodes'는 "실제"샘플에서 60 개가 넘는 요소를 가지고 있습니다 - 혼란 스럽습니다 - 주어진'trNode'에서 시작하면 ** 모든 자식 노드 **를 얻을 것입니다. 그 노드 - 전체 문서의 모든 ** 노드가 아님! –

답변

2

사용 자손 : 나는 HtmlAgility에 동의하지 않는

var linkNode = doc.DocumentNode.SelectSingle("//div[@id=\"content-wrapper\"]/dl/dd"); 
var hrefNode = linkNode.SelectSingleNode("descendant::a"); 

뭔가가 현재의 노드에서 해당 node.SelectNode의 * 호출 횡단 상단에서 DOM이 아닌 팩. 여기

는 경우

// table 
var tableNode = docNode.SelectSingleNode("//table"); 
// first tr 
var trNode = tableNode.SelectSingleNode("descendant::tr"); 

// you can also try, but it's overkill 
var trNode1 = tableNode.SelectSingleNode("descendant::tr[0]"); 

// then your td 
var tdNode = trNode.SelectSingleNode("descendant::td"); 
+0

전체 문서를 탐색하는 node.selectNode와 비슷한 문제가 생겼습니다. 나는 그것이 미래에 변하기를 바랍니다. –

+0

모든 AgilityPack 사용자는 새로운 행동을 채택하기 위해 코드를 변경해야 할 필요가 없으므로 나쁜 아이디어가 될 것입니다 ... 전 소프트웨어가 출시 되 자마자 기존 소프트웨어가 될 것이라고 생각합니다. – b0rg

관련 문제