2013-05-04 6 views
0

Yahoo Finance 페이지에서 주식 기호 및 회사 이름 목록을 구문 분석하려고합니다. 내가 사용하는 URL은 다음과 같습니다. http://uk.finance.yahoo.com/q/cp?s=%5EFTSEHTMLAgiltyPack의 구문 분석 결과

내가 사용하는 코드는 다음과 같습니다.

HtmlAgilityPack.HtmlDocument page = new HtmlWeb().Load("http://uk.finance.yahoo.com/q/cp?s=%5EFTSE"); 

     var titles = page.DocumentNode.SelectNodes("//td[@class='yfnc_tabledata1']"); 
    // Returns all titles on the home page of this site in an array. 

     foreach (var title in titles) 
     { 
      txtLog.AppendText(title.InnerHtml + System.Environment.NewLine); 

     } 

txtLog.AppendText 줄은 저만의 테스트입니다. 코드는 td의 노드 아래 yfnc_tabledata1 클래스를 포함하는 각 행을 올바르게 가져옵니다. 이제 foreach 루프에있을 때 다음 HTML에서 기호와 회사 이름을 가져 오기 위해 제목을 구문 분석해야합니다.

<b><a href="/q?s=GLEN.L">GLEN.L</a></b> 
GLENCORE XSTRAT 
<b>343.95</b> <nobr><small>3 May 16:35</small></nobr> 
<img width="10" height="14" style="margin-right:-2px;" border="0" 
src="http://l.yimg.com/os/mit/media/m/base/images/transparent-1093278.png" 
class="pos_arrow" alt="Up"> <b style="color:#008800;">12.80</b> 
<bstyle="color:#008800;"> (3.87%)</b> 68,086,160 

구문 분석 된 문서의 결과를 구문 분석 할 수 있습니까? 나는 어디서부터 시작해야할지 조금은 확신 할 수 없다.

+0

당신이 다시 부분 HTML을로드 시도 유무 :

가 XPATH에
HtmlAgilityPack.HtmlDocument page = new HtmlWeb().Load("http://uk.finance.yahoo.com/q/cp?s=%5EFTSE"); // get directly the symbols under the 1st TD element. Recursively search for an A element that has an HREF attribute under this TD. var symbols = page.DocumentNode.SelectNodes("//td[@class='yfnc_tabledata1']//a[@href]"); foreach (var symbol in symbols) { // from the current A element, go up two level and get the next TD element. var company = symbol.SelectSingleNode("../../following-sibling::td").InnerText.Trim(); Console.WriteLine(symbol.InnerText + ": " + company); } 

더 여기 축 : 여기 당신이 그것을 할 수있는 방법이다 (콘솔 응용 프로그램의 예에서 심볼과 기업의 목록을 덤프) 'HtmlDocument'? –

답변

0

사용자가있는 곳에서 XPATH 추출 작업을 계속하면됩니다. 많은 가능성이 있습니다. 모든 yfnc_tabledata1 노드가 동일한 수준에 있기 때문에 어려움이 있습니다. XPATH Axes