最近需要用到根据图片URL批量下载到本地的操作。查找了相关资料,记录在这儿。

1.首先在CSV文件中提取出url

    ifstream fin("C:\\Users\\lenovo\\Desktop\\query_result0503.csv"); //打开文件流操作
string line; int cnt = ;
while (getline(fin, line) && cnt < )
{
istringstream sin(line); //将整行字符串line读入到字符串流istringstream中
vector<string> urls;
string url;
while (getline(sin, url, ',')) //以逗号为分隔符
{
urls.push_back(url);
} cnt++;
size_t found = urls[].find_last_of("/\\");
if (found == string::npos) continue;
string imgname = urls[].substr(found+);
cout << "处理后:" << urls[] << "----" <<cnt<<"----" <<imgname << endl;
   }

2.根据URL将图片保存到本地。

需要用到 URLDownloadToFile函数 。

踩的坑主要是 这个函数需要用到宽字符参数。。如果参数类型不正确。。那么你甭想下载得到正确文件。。。

需要用到 MultiByteToWideChar函数来转换一下。。。。

HRESULT URLDownloadToFile(
LPUNKNOWN pCaller,
LPCTSTR szURL,
LPCTSTR szFileName,
_Reserved_ DWORD dwReserved,
LPBINDSTATUSCALLBACK lpfnCB
);

Parameters

  • pCaller
    A pointer to the controlling IUnknown interface of the calling ActiveX component, if the caller is an ActiveX component. If the calling application is not an ActiveX component, this value can be set to NULL. Otherwise, the caller is a COM object that is contained in another component, such as an ActiveX control in the context of an HTML page. This parameter represents the outermost IUnknown of the calling component. The function attempts the download in the context of the ActiveX client framework, and allows the caller container to receive callbacks on the progress of the download.

  • szURL
    A pointer to a string value that contains the URL to download. Cannot be set to NULL. If the URL is invalid, INET_E_DOWNLOAD_FAILURE is returned.

  • szFileName
    A pointer to a string value containing the name or full path of the file to create for the download. If szFileNameincludes a path, the target directory must already exist.

  • dwReserved
    Reserved. Must be set to 0.

  • lpfnCB
    A pointer to the IBindStatusCallback interface of the caller. By using IBindStatusCallback::OnProgress, a caller can receive download status. URLDownloadToFile calls the IBindStatusCallback::OnProgress and IBindStatusCallback::OnDataAvailable methods as data is received. The download operation can be canceled by returning E_ABORT from any callback. This parameter can be set to NULL if status is not required.

Return value

This function can return one of these values.

Return code Description
S_OK

The download started successfully.

E_OUTOFMEMORY

The buffer length is invalid, or there is insufficient memory to complete the operation.

INET_E_DOWNLOAD_FAILURE

The specified resource or callback interface was invalid.

///
size_t len = urls[].length();//获取字符串长度
int nmlen = MultiByteToWideChar(CP_ACP, , urls[].c_str(), len + , NULL, );//如果函数运行成功,并且cchWideChar为零,
//返回值是接收到待转换字符串的缓冲区所需求的宽字符数大小。
wchar_t* buffer = new wchar_t[nmlen];
MultiByteToWideChar(CP_ACP, , urls[].c_str(), len + , buffer, nmlen); string savepath = "C:\\Users\\lenovo\\Desktop\\csvfile\\query_result0423\\"+imgname;
size_t len1 = savepath.length();
wchar_t* imgsavepath = new wchar_t[len1];
int nmlen1 = MultiByteToWideChar(CP_ACP, , savepath.c_str(), len1 + , NULL, );
MultiByteToWideChar(CP_ACP, , savepath.c_str(), len1 + , imgsavepath, nmlen1); HRESULT hr = URLDownloadToFile(NULL, buffer, imgsavepath, , NULL);
if (hr == S_OK)
{
cout << "-------ok" << endl;
}

参考:

http://www.cnblogs.com/codingmengmeng/p/6258020.html

https://docs.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/platform-apis/ms775123(v=vs.85)#parameters

C++ 根据图片url 批量 下载图片的更多相关文章

  1. 在C#中使用正则表达式筛选出图片URL并下载图片URL中的图片到本地

    本功能主要用到的知识点如下: 1.正则表达式 2.C#中下载文件功能的实现 3.泛型集合的使用 4.进程的简单操作(用于结束当前程序) 下面就简单说一下是如何使用这些知识点的.先详细说下这个程序主要实 ...

  2. 根据url地址单个或批量下载图片

    我们在java开发的时候会遇到通过url地址下载图片的情况.方便起见,我把通过url地址下载图片封装了tool工具类,方便以后使用 1.根据如:http://abc.com/hotels/a.jpg  ...

  3. 【Python】nvshens按目录批量下载图片爬虫1.00(单线程版)

    # nvshens按目录批量下载图片爬虫1.00(单线程版) from bs4 import BeautifulSoup import requests import datetime import ...

  4. python图片爬虫 - 批量下载unsplash图片

    前言 unslpash绝对是找图的绝佳场所, 但是进网站等待图片加载真的令人捉急, 仿佛是一场拼RP的战争 然后就开始思考用爬虫帮我批量下载, 等下载完再挑选, 操作了一下不算很麻烦, 顺便也给大家提 ...

  5. URL地址下载图片到本地

    package test.dao; import eh.base.dao.DoctorDAO; import eh.entity.base.Doctor; import junit.framework ...

  6. scrapy操作mysql/批量下载图片

    1.操作mysql items.py meiju.py 3.piplines.py 4.settings.py -------------------------------------------- ...

  7. Java通过图片url地址获取图片base64位字符串的两种方式

    工作中遇到通过图片的url获取图片base64位的需求.一开始是用网上的方法,通过工具类Toolkit,虽然实现的代码比较简短,不过偶尔会遇到图片转成base64位不正确的情况,至今不知道为啥. 之后 ...

  8. python——批量下载图片

    前言 批量下载网页上的图片需要三个步骤: 获取网页的URL 获取网页上图片的URL 下载图片 例子 from html.parser import HTMLParser import urllib.r ...

  9. 用python批量下载图片

    一 写爬虫注意事项 网络上有不少有用的资源, 如果需要合理的用爬虫去爬取资源是合法的,但是注意不要越界,前一阶段有个公司因为一个程序员写了个爬虫,导致公司200多个人被抓,所以先进入正题之前了解下什么 ...

随机推荐

  1. 纸质文稿如何生成PDF

    步骤: (1) 将即将要转换的文稿单张向下放入打印机. (2) 将打印设备(打印机)连接至你的电脑. (3) 打开控制面板,点击"查看设备和打印机". (4) 找到你当前的打印设备 ...

  2. PHP绘制验证码

    <?php        //使用PHP绘图技术,画出自己的验证码 $checkCode="";    for($i=0;$i<4;$i++){             ...

  3. let与var的区别,为什么什么要用let?

    1.var是全局声明,let是块级作用的,只适用于当前代码块 var a = 1: if(true){ let a; a=22: console.log(a);'//22 } if(){}内就是let ...

  4. ssm多数据源的操作

    公司要求,需要使用两个数据库,一个mysql,一个oracle.所以需要配置两个数据库来进行操作. 1.首先,需要在jdbc.properties文件中将两个库的配置数据写入,不过一个写driver, ...

  5. MySQL+Service+Servlet+Jsp实现Table表格分页展示数据

    下面以一个示例讲解如何使用MySQL+Service+Servlet+Jsp实现Table表格分页展示数据: eg:请假管理系统 要求如下: 一.打开首页页面, 访问查询请假记录的 servlet , ...

  6. 使用tp3.2和mbUploadify.js上传图片的代码,记录一下

    HTML: <div class="form-group"> <label class="col-sm-1 control-label no-paddi ...

  7. 用户从地址栏输入url,按下enter键后,直到页面加载完成的这个过程都发生了什么?

    流程大概描述一下: 用户将url输入后,服务器接受到请求,然后将这个请求进行处理,然后将处理后的结果返回给浏览器,浏览器将该结果以页面的形式呈现给用户. 详细描述: 1:用户将url(例如www.ba ...

  8. WEB项目日志分析系统思考

    一.为什么需要日志分析系统 对ETL系统中数据转换和存储操作的相关日志进行记录以及实时分析有助于我们更好的观察和监控ETL系统的相关指标(如单位时间某些操作的处理时间),发现系统中出现的缺陷和性能瓶颈 ...

  9. [arc065E]Manhattan Compass[曼哈顿距离和切比雪夫距离转换]

    Description 传送门 Solution 题目要求的是曼达顿距离,对于每个点(x,y),我们把它变为(x-y,x+y),就可以转换成求切比雪夫距离了. 证明如下:$max(\left | (x ...

  10. 打豪车应用:uber详细攻略(附100元优步uber优惠码、uber优惠券、优步优惠码、优步优惠券)

    在嘀嘀打车和快的打车交战热闹的时候,美国的打车应用uber进入中国.与在美国以个人司机注册做 Uber 司机为主的模式不同,Uber 在中国采用与租车公司合作.由租车公司提供车辆和司机的模式,同时中文 ...