下载的页面源与呈现的页面源不同
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)中未找到。
所以我建议你在代码中添加一个步骤:
- 从支持命令行或某种API的javascript浏览器下载页面(如果curl不能做到这一点,也许wget或linux上的lynx/links/links2/elinks可以帮助你?)
- 解析数据。
相关文章:
- 下载URL中的所有文件
- VisualStudio:使用 Suse Enterprise Server 12 SP5 时,不会下载远程库标头
- 从 C++ 上的网址下载文件
- libcurl :显示正在运行的上传和下载速率
- 在 NVIDIA GEFORCE GTX 1050 上下载适用于 Windows 10 的 openCL 1.2
- 使用 libssh 从 SFTP 服务器下载文件并使用 C++ 将其写入 ofstream?
- 佳能SDK无法从相机下载图片
- 使用 Libcurl 库进行文件下载
- Mac os x app bundle 在下载并运行时崩溃,但在终端或更改 Info.plist 时运行良好
- 通过boost asio iostream下载大文件的最快方法是什么?
- 下载 GPU Mats 的矢量到主机
- 有没有办法使用 mongocxx 驱动程序从 GridFS 集合的文件下载任意范围?
- 为什么URLDownloadToFile()没有下载我的文件
- 下载到当前目录
- 顺序中止后未触发 Qt 下载进度
- 使用 Qt 下载公共保管箱文件
- WinInet只下载网页的一部分
- 视觉 无法使用 libcurl c++ 下载文件
- 下载文件到漫游问题
- 在 C++14 中的 for 循环中并行网络下载