C++ 根据图片url 批量 下载图片

本文介绍了一种从CSV文件中提取图片URL并批量下载的方法。使用C++代码实现了URL的提取与转换,通过URLDownloadToFile函数完成下载过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

 

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

 

    ifstream fin("C:\\Users\\lenovo\\Desktop\\query_result0503.csv"); //打开文件流操作  
    string line;

    int cnt = 0;
    while (getline(fin, line) && cnt < 20)  
    {
        istringstream sin(line); //将整行字符串line读入到字符串流istringstream中  
        vector<string> urls; 
        string url;
        while (getline(sin, url, ',')) //以逗号为分隔符  
        {
            urls.push_back(url); 
        }
        
        cnt++;
        size_t found =     urls[0].find_last_of("/\\");
        if (found == string::npos) continue;
        string imgname = urls[0].substr(found+1);
        cout << "处理后:" << urls[0] << "----" <<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 codeDescription
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[0].length();//获取字符串长度
        int nmlen = MultiByteToWideChar(CP_ACP, 0, urls[0].c_str(), len + 1, NULL, 0);//如果函数运行成功,并且cchWideChar为零,
                                                                                  //返回值是接收到待转换字符串的缓冲区所需求的宽字符数大小。
        wchar_t* buffer = new wchar_t[nmlen];
        MultiByteToWideChar(CP_ACP, 0, urls[0].c_str(), len + 1, 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, 0, savepath.c_str(), len1 + 1, NULL, 0);
        MultiByteToWideChar(CP_ACP, 0, savepath.c_str(), len1 + 1, imgsavepath, nmlen1);
        
        
        HRESULT hr = URLDownloadToFile(NULL, buffer, imgsavepath, 0, 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

转载于:https://www.cnblogs.com/hellowooorld/p/9005068.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值