2013-06-18 2 views
0

2 일 동안 고생했습니다. .NET 4.5 winforms 프로젝트에서 C# 및 HtmlAgilityPack을 사용하여 웹 사이트에서 데이터를 추출합니다 (추출 할 필드는 $ flow 및 B/S 비율). 나는 (흐름 :/n/t/t/t; 흐름 245 M 대신에) 나는 가치가 없다. 웹 페이지의 값을 보면서을 쿼리 할 때 왜 아무 값도 얻지 못합니다. 다른 사람이 노드의 이유를 찾았는지보고 싶습니다. = null 쿼리 결과. 다음은 쿼리 된 웹 페이지의 URL입니다. http://finance.avafin.com/tradeFlow?type=BS_RATIO&date=06%2F14%2F2013&alertId=0&symbol=spy&sectorId=0&industryId=0값을 반환하지 않는 HtmlAgilityPack 쿼리

위의 URL을 쿼리로 사용합니다.

아래의 방법을 사용했지만 다른 웹 페이지에서 다른 쿼리를 사용했음을 주목하십시오. 현재 쿼리에서 작동하지 않거나 현재 웹 페이지에 대한 필드 난독 처리가 의심되는 부분이 있습니다.

방법을 사용 : 쿼리하는

 /// <summary> 
     ///  Gets the data. 
     /// </summary> 
     /// <param name="url"> The URL. </param> 
     /// <returns> </returns> 
     public List<string> GetFlowData(string url) 
     { 
      // ('//a[contains(@href, "genre")]') 
      // <td class=" sorting_1">137.27B</td> 
      //*[@id="tf_data"]/tbody/tr[1]/td[8] // this is the xpath as seen in navigator for first value => I get no value when used as a query => (nodes = null) 
      //*[@id="tf_data"]/tbody/tr[1]/td[9] // this is the xpath as seen in navigator for second value => I get no value when used as a query => (nodes = null) 

// //td[@class=''] => nodes null too 


      // I see the b/s ratio node in body but no value /n/ttt instead using [@id='tf_data']/tbody 
      var nodes = LoadHtmlDoc(url, "//*[@id='tf_data']/tbody"); 
      List<string> tickers = new List<string>(); 
      if (nodes == null) 
      { 
       return new List<string> { "Ticker not available" }; 
      } 
      int i = 0; 
      foreach (var v in nodes) 
      { 
       i++; 

        MessageBox.Show(v.InnerText + " " + i.ToString()); 
       //// The placement of the data containing bought/sold ratio 
       //if (i == 7) 
       //{ 
       // tickers.Add(v.InnerText); 
       //} 
       //// The placement of the data containing $ Flow 
       //if (i == 8) 
       //{ 
       // tickers.Add(CleanFlowData(v.InnerText)); 
       //} 
      } 

      return tickers; 
     } 
+0

loadhtml을 만들었고 정상적으로 작동하면 html 문서가 올바르게로드됩니다. –

답변

0

페이지 ID th_data와 테이블의 데이터가 포함되어 있지 않습니다. 이 페이지의 마크 업을 검사 할 경우, 당신은 볼 수 있습니다 : 문서가로드 된 후 모든 데이터는 자바 스크립트를 통해 브라우저가이 테이블에 추가됩니다

<table cellpadding="0" cellspacing="0" border="0" class="display" id="tf_data"> 
    <thead> 
     <tr height="10"> 
      <th align="center"></th> 
      <th align="center" width="90">CHART</th> 
      <th align="left" width="70">SYMBOL</th> 
      <th align="left">MARKET CAP</th> 
      <th align="right" width="65">PRICE</th> 
      <th align="center" width="80">CHANGE</th> 
      <th align="right">VOL</th> 
      <th align="right">B/S RATIO</th> 
      <th align="right" width="80">NET CASH FLOW</th> 
     </tr> 
    </thead> 
    <tbody> <-- empty! 
    </tbody> 
</table> 

($(document).ready 기능 참조). 따라서 해당 URL에서 HTML을 가져 오는 경우 브라우저에서 Java 스크립트 코드를 실행할 때까지 데이터가 없습니다. 나는. 당신이 분석 할 수있는 것이 아무것도 없습니다.

JSON 데이터를 페이지로로드하는 스크립트를 살펴보고 코드에서 동일한 서비스를 호출하는 것이 좋습니다.


질문 범위 밖으로 그것의, 그러나 데이터를 검색하면 System.Net.Http 어셈블리에서 HttpClient 클래스를 사용할 수 있습니다. 다음은 사용의 예제는 (당신의 최대 쿼리 문자열이 구성되는 방식을 분석하는) :

HttpClient client = new HttpClient(); 
client.BaseAddress = new Uri("http://finance.avafin.com"); 
string url = "data?sEcho=2&iColumns=9&sColumns=&iDisplayStart=0&iDisplayLength=20&mDataProp_0=0&mDataProp_1=1&mDataProp_2=2&mDataProp_3=3&mDataProp_4=4&mDataProp_5=5&mDataProp_6=6&mDataProp_7=7&mDataProp_8=8&sSearch=&bRegex=false&sSearch_0=&bRegex_0=false&bSearchable_0=true&sSearch_1=&bRegex_1=false&bSearchable_1=true&sSearch_2=&bRegex_2=false&bSearchable_2=true&sSearch_3=&bRegex_3=false&bSearchable_3=true&sSearch_4=&bRegex_4=false&bSearchable_4=true&sSearch_5=&bRegex_5=false&bSearchable_5=true&sSearch_6=&bRegex_6=false&bSearchable_6=true&sSearch_7=&bRegex_7=false&bSearchable_7=true&sSearch_8=&bRegex_8=false&bSearchable_8=true&iSortCol_0=4&sSortDir_0=asc&iSortingCols=1&bSortable_0=true&bSortable_1=true&bSortable_2=true&bSortable_3=true&bSortable_4=true&bSortable_5=true&bSortable_6=true&bSortable_7=true&bSortable_8=true&type=BS_RATIO&date=06%2F14%2F2013&categoryName=&alertId=0&alertId2=&industryId=0&sectorId=0&symbol=spy&recom=&period=&perfPercent="; 
var response = client.GetStringAsync(url).Result; 

응답은 구문 분석 할 수있는 HTML을 포함합니다.

+0

OK, json 서비스를 호출 할 수있는 방법은 무엇입니까? 나는 '$ .getJSON'과 함께 js 줄을 보았다. JasonValue.Parse (webClient.DownloadString (url);? –

+0

@TeycirBenSoltane 업데이트 참조 –

+0

감사합니다 lazyberezovsky –

관련 문제