【发布时间】: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。现在它起作用了。我不知道发生了什么。