下载的页面源与呈现的页面源不同

Downloaded page source is different than the rendered page source

本文关键字:下载      更新时间:2023-10-16

我打算从这个网站获取数据

http://www.gpw.pl/akcje_i_pda_notowania_ciagle

(这是波兰主要股票市场的网站)

我有一个用c++写的程序,下载网站的源代码到文件。但问题是里面没有我感兴趣的东西(当然是股票的价值)。

如果你比较这个网站的来源选项"查看元素"(RMB ->查看元素)你可以看到"View element"确实包含了股票的值。

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

等等……

网站的下载源没有这些信息。

我们有两个问题

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 urln" << 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;
    }  

因为这些值是用JavaScript填写的。

"查看源代码"显示的是页面的原始源代码,而"查看元素"显示的是文档树当前的状态。

没有简单的方法来修复它,因为您需要执行JavaScript或将其移植到c++(这可能会使您在交易所不受欢迎)。

当我将页面保存为html文件(file/save as)时,我得到一个文件,其中包含浏览器中显示的所有数据,并且在页面源(我使用Chrome)中未找到。

所以我建议你在代码中添加一个步骤:

  1. 从支持命令行或某种API的javascript浏览器下载页面(如果curl不能做到这一点,也许wget或linux上的lynx/links/links2/elinks可以帮助你?)
  2. 解析数据。