2013-06-18 89 views
0

一直在掙扎2天。我在.NET 4.5 winforms項目中使用C#和HtmlAgilityPack從網站提取數據(我想提取的字段是$ flow和B/S比率)。 我到達現場(流量:/ n/t/t/t;而不是流量245 M),但我沒有任何價值。 我不知道爲什麼我查詢時我沒有得到任何價值,而我看到網頁中的值。想查看是否有其他人發現我的查詢結果= null的原因。 這是athe查詢網頁的網址:http://finance.avafin.com/tradeFlow?type=BS_RATIO&date=06%2F14%2F2013&alertId=0&symbol=spy&sectorId=0&industryId=0HtmlAgilityPack查詢返回沒有值

我使用上面的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> 

所有的數據文件被加載後,由瀏覽器通過Java腳本添加到此表(見$(document).ready功能)。因此,如果您從該網址獲取html,那麼在瀏覽器運行Java Script代碼之前不會有數據。即沒有什麼可以解析的。

我建議你檢查將JSON數據加載到頁面中的腳本,並簡單地從代碼中調用相同的服務。


它的出題範圍,但是對於檢索數據,您可以使用HttpClient類從System.Net.Http裝配。下面是使用的樣品(其由你來分析查詢字符串應該如何組成):

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

好吧,任何建議如何我可以調用json服務?我看到一些帶有'.getJSON'的js行; JsonValue.Parse(webClient.DownloadString(url);? –

+0

@TeycirBenSoltane請參閱更新 –

+0

感謝lazyberezovsky –