2015-02-05 2 views
0

위키 피 디아 웹 사이트의 테이블에서 데이터를 긁어 내려고했지만 지금까지 참조 할 노드를 찾았습니다. Wikipedia의 표에는 많은 수의 항목이 있지만 앱을 실행하면 12 개의 결과 만 표시되며 모두 동일합니다. 반환 된 모든 결과는 테이블의 첫 번째 항목과 중복됩니다.Htmlagilitypack을 사용하여 위키 피 디아에서 데이터 스크랩하기

수정 방법에 대한 아이디어가 있으십니까?

protected async override void OnNavigatedTo(NavigationEventArgs e) 
{ 
    base.OnNavigatedTo(e); 
    string htmlPage = ""; 
    { 
     htmlPage = await client.GetStringAsync("http://en.wikipedia.org/wiki/List_of_Games_with_Gold_games"); 
    } 

HtmlDocument htmlDocument = new HtmlDocument(); 
htmlDocument.LoadHtml(htmlPage); 

foreach (var div in htmlDocument.DocumentNode.SelectNodes(".//h2")) 
{ 
    GameHistory newGameHistory = new GameHistory(); 
    newGameHistory.historyTitle = div.SelectSingleNode("//i//a").InnerText.Trim(); 
    newGameHistory.historyAdded = div.SelectSingleNode("//span[starts-with(@style, 'white')]").InnerText.Trim(); 
    newGameHistory.historyRemoved = div.SelectSingleNode("(//span[starts-with(@style, 'white')])[2]").InnerText.Trim(); 
    gameHistory.Add(newGameHistory); 
    } 
lstGameHistory.ItemsSource = gameHistory; 
} 

답변

0

귀하의 XPath를 완전히 옳지 않다가 ...

foreach (var div in htmlDocument.DocumentNode.SelectNodes(".//h2")) 
{ 
    GameHistory newGameHistory = new GameHistory(); 
    newGameHistory.historyTitle = div.SelectSingleNode("//i//a").InnerText.Trim(); 
    newGameHistory.historyAdded = div.SelectSingleNode("//span[starts-with(@style, 'white')]").InnerText.Trim(); 
    newGameHistory.historyRemoved = div.SelectSingleNode("(//span[starts-with(@style, 'white')])[2]").InnerText.Trim(); 
    gameHistory.Add(newGameHistory); 
} 

은 내가 H2 태그가 "말하고있다. 나 내부 태그로 내가 모든 태그를하자 그리고 스팬 태그 ... h2 태그와는 아무 관련이 없습니다. 전체 문서에서 첫 번째 태그를 계속 사용하도록하겠습니다. " (즉, 이중 슬래시가 의미하는 것).

얼마나 많은 태그가 있습니까? h2 개의 태그가 12 개 있습니다.

어쨌든 h2 태그를 참조로 사용했다고해도 행을 보는 것과 관련이없는 것처럼 보입니다.

올바른 테이블 (이 경우 테이블)의 각 행을 가져올 XPath를 얻는 것이 필요합니다. 그리고 각 행에 대해 XPath는 "."문자로 시작해야합니다. (자기), 그래서 당신은 다시 문서의 루트로 돌아 가지 않을거야.

또한 게임에는 "삭제됨"열이 없으므로이를 처리해야합니다.

짜잔 내 코드 :

foreach (var div in htmlDocument.DocumentNode.SelectNodes("//table[@class='wikitable sortable']/tr[td/i/a]")) 
    { 
     GameHistory newGameHistory = new GameHistory(); 
     newGameHistory.historyTitle = div.SelectSingleNode(".//i//a").InnerText.Trim(); 
     newGameHistory.historyAdded = div.SelectSingleNode(".//span[starts-with(@style, 'white')]").InnerText.Trim(); 
     newGameHistory.historyRemoved = div.SelectSingleNode("(.//span[starts-with(@style, 'white')])[2]") != null? div.SelectSingleNode("(.//span[starts-with(@style, 'white')])[2]").InnerText.Trim() : string.Empty; 
     gameHistory.Add(newGameHistory); 
    } 

팁 : foreach 루프 내부에, 한 번 (..)를 테이블로 이동하도록 이동합니다 (TR부터), 제목을 얻으려면 태그를 입력 한 다음테이블이전의 태그 인 h2를 얻으려면 이전 형제를 사용하십시오.

따라서 XPath는 "../preceding-sibling::h2"이됩니다. h2가 다른 문자를 캡처하는 것처럼 보이므로 XPath를 더 세분화해야합니다.

+0

감사합니다. Tyress, 완벽하게 작동합니다! 가능한 한 다른 것. h2 태그에 포함 된 제목은 Xbox 360 또는 Xbox One의 제목으로 두 개의 개별 목록과 구별됩니다. 어떻게 통합 할 수 있습니까? 모든 게임에 Xbox 360/One 중 하나를 사용하거나 목록을 분할하려면 한 번만 사용하는 것이 좋습니다. 다시 한번 감사드립니다. –

+0

답변을 업데이트하겠습니다. @kkerswell – Tyress

+0

Tyress에게 감사드립니다. 나는 마치 "../preceding-sibling::h2//span[@id='XX_One ']" –

관련 문제