C++ URL Download

1-找到对应网页,F12查看源码,找到图片缩略图对应的内容,copy生成txt文件,作为读入文件
2-通过word的正则修改获得图片下载URL集(我这里直接使用了字符查找)
3-使用urlmon库里的downlode循环下载


```cpp
#include <tchar.h>
#include <iostream>
#include <urlmon.h>
#include <string>
#include <fstream>
#pragma comment(lib,"urlmon.lib")
using namespace std;
void testByChar()
{
	fstream testByCharFile;
	char c_old = ' ';char c_pre=' ';
	testByCharFile.open("1.txt", ios::in);
	string str1("");string str2("");string str3(""); string str4("");
	bool getStr1 = false;bool getStr2 = false;bool getStr3 = false; bool getStr4 = false;
	bool exit_inner = false; bool exit = false;
	string str;
	string url("");
	int num = 1;
	while (!exit) {
		//--------------------------------------------------
		//使用类正则逻辑进行链接修改
		while (!exit_inner)
		{
			testByCharFile >> c_pre;
			if (c_pre == '>'&&c_old == 'i') {
				exit_inner = true; break;
			}
			if (c_pre == '@'&&c_old == '@') {
				exit = true; break;
			}
			if (c_pre == '/'&&c_old == '/') getStr1 = true;
			if (c_pre == 'i'&&c_old == '?') getStr1 = false;
			if (getStr1)
			{
				str.assign(1, c_pre);
				str1.append(str);
			}
			if (c_pre == 's'&&c_old == ';') getStr2 = true;
			if (c_pre == 'a'&&c_old == '&') getStr2 = false;
			if (getStr2)
			{
				str.assign(1, c_pre);
				str2.append(str);
			}
			if (c_pre == 't'&&c_old == ';') getStr3 = true;
			if (c_pre == 'q'&&c_old == '&') getStr3 = false;
			if (getStr3)
			{
				str.assign(1, c_pre);
				str3.append(str);
			}
			if (c_pre == 'A'&&c_old == '/') getStr4 = true;
			if (c_pre == 'w'&&c_old == '_') getStr4 = false;
			if (getStr4)
			{
				str.assign(1, c_pre);
				str4.append(str);
			}
			c_old = c_pre;
		}
		if (!exit) {
			url.append("https:/").append(str1).append(str2).append(str3).append("attname=").append(str4);
			url.pop_back();
			url.append(".jpg");
			cout << url << endl;
		//----------------------------------------------------------------------------------------
			//通过链接下载对应文件
			size_t len = url.length();//获取字符串长度
			int nmlen = MultiByteToWideChar(CP_ACP, 0, url.c_str(), len + 1, NULL, 0);//如果函数运行成功,并且cchWideChar为零,
			//返回值是接收到待转换字符串的缓冲区所需求的宽字符数大小。
			wchar_t* buffer = new wchar_t[nmlen];
			MultiByteToWideChar(CP_ACP, 0, url.c_str(), len + 1, buffer, nmlen);
			string name = "E:";
			name.append(to_string(num));
			name.append(".jpg");
			HRESULT hr = URLDownloadToFile(NULL, url.c_str(), _T(name.c_str()), 0, NULL);
			num++;
			if (hr == S_OK)
			{
				cout << "ok" << endl;
			}
			delete[] buffer;
			str1.clear(); str2.clear(); str3.clear(); str4.clear(); url.clear();
		}
		exit_inner = false;
	}
	testByCharFile.close();
}
int main()
{
	testByChar();
	return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值