使用Libcurl C 从Twitter下载图像
Downloading an image from twitter with libcurl C++
我正在制作一个获取一个人的推文的程序,如果包含图像,请下载它。
为什么我可以从此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&amgrefurl=htttts e
示例#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;
}
首先,这不是图像的链接。这是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");
添加了两个选项后,我设法下载了您的链接。
相关文章:
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 如何使用OpenCV将RBG图像转换为HSV,并将H、S和V值保存为C++中的3个独立图像
- OpenCV EqualizeHist()从彩色图像创建黑白图像
- 将"打开的CV图像"中的"颜色"转换为整数格式
- 平均图像时图像损坏
- 在C++中使用GDAL可以将图像的像素坐标转换为lat,long吗
- 使用Libcurl C 从Twitter下载图像
- 如何避免在 c++ 中使用 libssh scp 命令压缩从远程 ssh 下载的图像、音频和视频文件?
- Boost:Asio.从服务器下载图像文件
- 试图在BlackBerry Cascades QT和QML下载图像
- C QTNETWORKREQUEST下载图像,但获得了HTML
- 在c++中使用curl从url下载图像
- 从服务器下载图像(cUrl,但需要建议)C++
- Canon SDK-下载图像到主机
- 在哪里下载BOOST通用图像库的数字扩展
- 使用 C++/boost::asio/libcurl 的简单代理 - 无法下载图像
- 下载一个图像样本
- cURL下载相机的图像(http请求)
- 如何通过http下载和保存图像
- Qt -从网络下载图像