gpt4 book ai didi

c++ - 从 Cpp 中的 URL 下载时获取空的 CSV

转载 作者:行者123 更新时间:2023-12-02 10:13:05 24 4
gpt4 key购买 nike

我的目标是使用 Cpp 从特定 URL 获取 CSV 或 XLS。
打开以下链接时

http://www.centrodeinformacao.ren.pt/userControls/GetExcel.aspx?T=CRG&P=01-01-2007&variation=PT
,可以在浏览器工具中看到
Browser redirect
302 重定向和实际从以下 URL 下载的文件
http://www.centrodeinformacao.ren.pt/_layouts/CI.GetExcel/SafeGetExcel.aspx?T=CRG&P=02-01-2007&variation=PT
如下图所示(请求 URL)
Request URL
如果我手动转到这两个链接中的任何一个,.xls 文件下载就好了,所以我们不妨在重定向后使用那个。

我决定继续使用 libcurl在我的 W10 机器上使用 Visual Studio 2017。在 Visual Studio 2017 项目中包含 libcurl 的推荐方法是使用 vcpkg这就是我使用的。

1.安装vcpkg
  • 打开 Git Bash,cd C:/Program Files/并克隆 this repo .

  • Clone repo
  • 打开命令提示符,cd C:/Program Files/vcpkg,运行bootstrap-vcpkg.bat

  • bootstrap-vcpkg.bat
    运行后 vcpkg integrate install vcpkg integrate install

    2.安装libcurl
  • 运行vcpkg install curl

  • vcpkg install curl

    3.创建一个新项目
  • 只需创建 Visual C++ > Windows 桌面 > Windows 控制台应用程序

  • Windows Console Application
    能够使用 #include <curl/curl.h>马上
    use #include <curl/curl.h>

    4. 当前结果
    然后,受到以下答案的启发
  • https://stackoverflow.com/a/6951203/9415908
  • https://stackoverflow.com/a/1636415/9415908

  • 并使用以下代码
    #include "pch.h"
    #define _CRT_SECURE_NO_WARNINGS
    #include <iostream>
    #include <iostream>
    #include <stdio.h>
    #include <curl/curl.h>
    #include <string.h>

    size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream) {
    size_t written = fwrite(ptr, size, nmemb, stream);
    return written;
    }

    void downloadFile(const char* url, const char* fname) {
    CURL *curl;
    FILE *fp;
    CURLcode res;
    curl = curl_easy_init();
    if (curl) {
    fp = fopen(fname, "wb");
    curl_easy_setopt(curl, CURLOPT_URL, url);
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
    res = curl_easy_perform(curl);
    curl_easy_cleanup(curl);
    fclose(fp);
    }
    }

    int main(void) {

    downloadFile("http://www.centrodeinformacao.ren.pt/_layouts/CI.GetExcel/SafeGetExcel.aspx?T=CRG&P=01-01-2007&variation=PT", "C:\\Users\\molecoder\\Desktop\\test.csv");

    }
    我可以在所需的文件夹中看到一个 test.csv,但它是一个空文件。
    empty CSV

    最佳答案

    转到该特定 URL 后,将下载一个 .xls 文件。我不介意使用 XLS 而不是 CSV,因此将其更改为我能够按预期获取文件。

    downloadFile("http://www.centrodeinformacao.ren.pt/_layouts/CI.GetExcel/SafeGetExcel.aspx?T=CRG&P=01-01-2007&variation=PT", "C:\\Users\\molecoder\\Desktop\\test.xls");
    It works
    这是最终代码
    #include "pch.h"
    #define _CRT_SECURE_NO_WARNINGS
    #include <iostream>
    #include <iostream>
    #include <stdio.h>
    #include <curl/curl.h>
    #include <string.h>

    size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream) {
    size_t written = fwrite(ptr, size, nmemb, stream);
    return written;
    }

    void downloadFile(const char* url, const char* fname) {
    CURL *curl;
    FILE *fp;
    CURLcode res;
    curl = curl_easy_init();
    if (curl) {
    fp = fopen(fname, "wb");
    curl_easy_setopt(curl, CURLOPT_URL, url);
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
    res = curl_easy_perform(curl);
    curl_easy_cleanup(curl);
    fclose(fp);
    }
    }

    int main(void) {

    downloadFile("http://www.centrodeinformacao.ren.pt/_layouts/CI.GetExcel/SafeGetExcel.aspx?T=CRG&P=01-01-2007&variation=PT", "C:\\Users\\molecoder\\Desktop\\test.xls");

    }

    关于c++ - 从 Cpp 中的 URL 下载时获取空的 CSV,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62850908/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com