2013-05-31 81 views
1

我打算從該網站獲得的數據不同下載頁面源比呈現的頁面源

http://www.gpw.pl/akcje_i_pda_notowania_ciagle

(它的主要股市在波蘭的網站)

我我已經得到了一個用C++編寫的程序,它將網站的源文件下載到文件中。 但問題是,它不包含我感興趣的東西 (當然是股票的價值)。

如果您將該網站的來源與「查看元素」(RMB - >查看元素)選項 進行比較,您可以看到「查看元素」確實包含了股票的值。

<td>75.6</td> 
<tr class="even red"> 

等等...

該網站的下載源沒有這些信息。

所以我們有2個問題

1)爲什麼該網站的源是從「視圖元素」選項有什麼不同?

2)如何轉移我的程序,以便它可以下載正確的代碼?

#include <string> 
    #include <iostream> 
    #include "curl/curl.h" 
    #include <cstdlib> 

    using namespace std; 

    // Write any errors in here 
    static char errorBuffer[CURL_ERROR_SIZE]; 

    // Write all expected data in here 
    static string buffer; 

    // This is the writer call back function used by curl 
    static int writer(char *data, size_t size, size_t nmemb, 
         string *buffer) 
    { 
     // What we will return 
     int result = 0; 

     // Is there anything in the buffer? 
     if (buffer != NULL) 
     { 
     // Append the data to the buffer 
     buffer->append(data, size * nmemb); 

     // How much did we write? 
     result = size * nmemb; 
     } 

     return result; 
    } 

    // You know what this does.. 
    void usage() 
    { 
     cout <<"curltest: \n" << endl; 
     cout << "Usage: curltest url\n" << endl; 
    } 

    /* 
    * The old favorite 
    */ 
    int main(int argc, char* argv[]) 
    { 
     if (argc > 1) 
     { 
     string url(argv[1]); 

     cout<<"Retrieving "<< url << endl; 

     // Our curl objects 
     CURL *curl; 
     CURLcode result; 

     // Create our curl handle 
     curl = curl_easy_init(); 

     if (curl) 
     { 
      // Now set up all of the curl options 
      curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errorBuffer); 
      curl_easy_setopt(curl, CURLOPT_URL, argv[1]); 
      curl_easy_setopt(curl, CURLOPT_HEADER, 0); 
      curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1); 
      curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writer); 
      curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer); 

      // Attempt to retrieve the remote page 
      result = curl_easy_perform(curl); 

      // Always cleanup 
      curl_easy_cleanup(curl); 

      // Did we succeed? 
      if (result == CURLE_OK) 
      { 
      cout << buffer << "\n"; 
      exit(0); 
      } 
      else 
      { 
      cout << "Error: [" << result << "] - " << errorBuffer; 
      exit(-1); 
      } 
     } 
     } 
     return 0; 
    } 
+0

_如何轉移我的程序,以便它可以下載正確的代碼?_:您的程序看起來非常接近CURL附帶的示例... –

回答

0

因爲這些值是使用JavaScript填充的。

「查看源代碼」顯示了頁面的原始來源,而「查看元素」顯示了此時文檔樹所處的狀態。

有沒有簡單的方法來解決它,因爲你需要執行JavaScript或將其移植到C++(它可能會讓你在交易所不受歡迎)。

0

當我將頁面保存爲html文件(文件/另存爲)時,我得到一個文件,其中包含瀏覽器中顯示的所有數據以及在頁面源代碼中找不到的(我使用Chrome)。

所以我建議你在你的代碼添加一個步驟:

  1. 下載從支持命令行或某種API的Javascript的瀏覽器頁面(如果捲曲不能做到這一點,也許在Linux上的wget或lynx/links/links2/elinks可以幫助你?)。
  2. 解析數據。