【问题标题】:Downloading an image from twitter with libcurl C++使用 libcurl C++ 从 twitter 下载图像
【发布时间】:2017-12-27 03:00:39
【问题描述】:

我正在制作一个程序来获取一个人的推文,如果它包含图像,请下载它。

为什么我可以从此 URL 下载图像(示例 #1)
http://www.google.co.id/intl/en_com/images/logo_plain.png
而不是来自这个 URL(示例 #2)
https://www.google.com/imgres?imgurl=https://pbs.twimg.com/media/DR-kkH4XcAAQ-vc.jpg&imgrefurl=https://twitter.com/officialmcafee/status/945655402276024320&h=1200&w=992&tbnid=0q3B6ZB_UxjRIM&tbnh=247&tbnw=204&usg=__xvjbjSSMvuImESBLVvBBrUagUe8=&docid=vdqkoUmaefYoFM

示例 #1

#include <iostream> 
#include <curl/curl.h> 

using namespace std;
int main()
{
    CURL *image;
    CURLcode imgresult;
    FILE *fp = nullptr;
    const char *url = "http://www.google.co.id/intl/en_com/images/logo_plain.png";
    image = curl_easy_init();
    if (image)
    {
        // Open file 
        fp = fopen("img.png", "wb");
        if (fp == NULL) cout << "File cannot be opened";

        curl_easy_setopt(image, CURLOPT_WRITEFUNCTION, NULL);
        curl_easy_setopt(image, CURLOPT_WRITEDATA, fp);
        curl_easy_setopt(image, CURLOPT_URL, url);
        // Grab image 
        imgresult = curl_easy_perform(image);
        if (imgresult)
            cout << "Cannot grab the image!\n";
    }
    // Clean up the resources 
    curl_easy_cleanup(image);
    // Close the file 
    fclose(fp);
    system("pause");
    return 0;
}

示例 #2

#include <iostream> 
#include <curl/curl.h> 

using namespace std;
int main()
{
    CURL *image;
    CURLcode imgresult;
    FILE *fp = nullptr;
    const char *url = "https://www.google.com/imgres?imgurl=https://pbs.twimg.com/media/DR-kkH4XcAAQ-vc.jpg&imgrefurl=https://twitter.com/officialmcafee/status/945655402276024320&h=1200&w=992&tbnid=0q3B6ZB_UxjRIM&tbnh=247&tbnw=204&usg=__xvjbjSSMvuImESBLVvBBrUagUe8=&docid=vdqkoUmaefYoFM";
    image = curl_easy_init();
    if (image)
    {
        // Open file 
        fp = fopen("img.png", "wb");
        if (fp == NULL) cout << "File cannot be opened";

        curl_easy_setopt(image, CURLOPT_WRITEFUNCTION, NULL);
        curl_easy_setopt(image, CURLOPT_WRITEDATA, fp);
        curl_easy_setopt(image, CURLOPT_URL, url);
        // Grab image 
        imgresult = curl_easy_perform(image);
        if (imgresult)
            cout << "Cannot grab the image!\n";
    }
    // Clean up the resources 
    curl_easy_cleanup(image);
    // Close the file 
    fclose(fp);
    system("pause");
    return 0;
}

【问题讨论】:

  • 很可能是因为您显然收到了来自 google 的 HTTP 重定向。不熟悉 libcurl,但可能有一个选项可以设置为自动跟随HTTP 重定向。如果没有,您将不得不做更多工作以从 Google 的响应中提取真实 URL,并尝试从真实 URL 下载。
  • @SamVarshavchik 但是即使我使用原始图像的地址:pbs.twimg.com/media/DR-kkH4XcAAQ-vc.jpg 它仍然不起作用
  • 我没有任何问题,无论如何,使用 curl 命令行客户端从您在评论中包含的 URL 下载图像。就像我说的,我对 libcurl 不熟悉,简单的谷歌搜索找到了 libcurl 的文档,在阅读了curl_easy_setopt() 的文档并查看了您的代码之后,您为什么不下载任何东西似乎很明显。这就是你告诉图书馆要做的事情:不要下载任何东西。您将CURLOPT_WRITEFUNCTION 设置为NULL。似乎您告诉图书馆忽略它下载的所有内容(没有写入功能)。那么您期望会发生什么?
  • 我不知道你在做什么,但是我更改了 CURLOPT_WRITEFUNCTION 仍然无法下载。
  • 刚刚将其切换回 NULL。现在它起作用了。我不知道发生了什么。

标签: c++ curl twitter


【解决方案1】:

首先,这不是图片链接。这是html页面。请注意,您的代码不会下载 html 页面引用的任何图像,而只是 html 页面。

其次,您没有关注重定向。再添加一个选项:

curl_easy_setopt(image, CURLOPT_FOLLOWLOCATION, 1);

第三,你最好伪装成浏览器:

curl_easy_setopt(image, CURLOPT_USERAGENT, "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36");

添加这两个选项后,我设法下载了您的链接。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-07
    • 2010-12-10
    • 2011-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多