您的位置:首页 > 编程语言 > C语言/C++

c++ 调用 wget 下载网页图片

2015-03-15 17:53 519 查看
这是一个c++调用wget下载图片的小程序, 可以结合我前一篇博客, 自动下载网页图片。

#include <stdlib.h>
#include <stdio.h>

#include <iostream>  
using namespace std;  

#include <windows.h>  

#include <string>  
#include <vector>  

//file name max length  
#define getFolderFile_max_length 1024  

//dfs  
bool dfsFolder(string rootPath, vector<string> &fileList, vector<FILETIME> &timelist)  
{  
	WIN32_FIND_DATA findData;  
	HANDLE hError;  

	//构造路径  
	char nowPath[getFolderFile_max_length];  
	strcpy(nowPath, rootPath.c_str());  
	strcat(nowPath, "\\*.*");  

	//文件名  
	char fileName[getFolderFile_max_length];  

	hError = FindFirstFile(nowPath, &findData);  
	if (hError == INVALID_HANDLE_VALUE)  
	{  
		printf("搜索失败!");  
		return false;  
	}  

	while (::FindNextFile(hError, &findData))  
	{  
		//过虑.和..  
		if (strcmp(findData.cFileName, ".") == 0  
			||strcmp(findData.cFileName, "..") == 0)  
			continue;  

		// FILETIME ftCreate, ftAccess, ftWrite; 
		// GetFileTime(hError, &ftCreate, &ftAccess, &ftWrite); 
		FILETIME ttt = findData.ftLastWriteTime;
		timelist.push_back(ttt);

		//构造完整文件名  
		sprintf(fileName, "%s\\%s", rootPath.c_str(), findData.cFileName);  
		fileList.push_back(string(fileName));  

		//深度遍历递归,不是&&,可能是与操作  
		if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)  
		{  
			//弹出文件夹  
			fileList.pop_back();  

			// fileList.push_back(string(fileName));  

			dfsFolder(string(fileName), fileList, timelist);  
		}  
	}  

	return true;  
}  

void bubblesort(vector<string> &fileList, vector<FILETIME> &unsorted)
{
	for (int i = 0; i < unsorted.size(); i++)
	{
		for (int j = i; j < unsorted.size(); j++)
		{
			if (CompareFileTime(&unsorted[i], &unsorted[j]) == 1)
			{
				FILETIME temp = unsorted[i];
				unsorted[i] = unsorted[j];
				unsorted[j] = temp;

				string temp2 = fileList[i];
				fileList[i] = fileList[j];
				fileList[j] = temp2;
			}
		}
	}
}

int main()
{
	string path = "1";

	int beginindex = 1;
	

	vector<string> filefolderlist;
	vector<FILETIME> timelist;

	dfsFolder(path, filefolderlist, timelist);

	bubblesort(filefolderlist, timelist);

	for (int i = 0; i< filefolderlist.size(); i++)
	{
		int index = beginindex+i-1;

		string name = filefolderlist[i];

		string truename = name.substr(name.find_last_of('\\')+1, name.find_last_of('.')-1 - name.find_last_of('\\'));

		char cmd[256];

		sprintf(cmd, "mkdir \"%d_%s\"", index, truename.c_str());
		int su = system(cmd);

		if (!su)
		{
			sprintf(cmd, "wget -P \"%d_%s\" -i \"%s\"", index, truename.c_str(), filefolderlist[i].c_str());

			system(cmd);
		}
		else
		{

			cout<< index<<filefolderlist[i]<<endl;

			system("pause");
		}
		

	}

	return 0;
}


这是1.txt

http://i0.wp.com/heru3.com/wp/wp-content/uploads/2014/12/web_09462.jpg http://i0.wp.com/heru3.com/wp/wp-content/uploads/2014/12/web_09462.jpg http://i1.wp.com/heru3.com/wp/wp-content/uploads/2014/12/web_09495.jpg http://i1.wp.com/heru3.com/wp/wp-content/uploads/2014/12/web_09495.jpg http://i2.wp.com/heru3.com/wp/wp-content/uploads/2014/12/web_09504.jpg http://i2.wp.com/heru3.com/wp/wp-content/uploads/2014/12/web_09504.jpg http://i1.wp.com/heru3.com/wp/wp-content/uploads/2014/12/web_09506.jpg http://i1.wp.com/heru3.com/wp/wp-content/uploads/2014/12/web_09506.jpg http://i1.wp.com/heru3.com/wp/wp-content/uploads/2014/12/web_09510.jpg http://i1.wp.com/heru3.com/wp/wp-content/uploads/2014/12/web_09510.jpg http://i0.wp.com/heru3.com/wp/wp-content/uploads/2014/12/web_09514.jpg http://i0.wp.com/heru3.com/wp/wp-content/uploads/2014/12/web_09514.jpg http://i2.wp.com/heru3.com/wp/wp-content/uploads/2014/12/web_09522.jpg http://i2.wp.com/heru3.com/wp/wp-content/uploads/2014/12/web_09522.jpg http://i1.wp.com/heru3.com/wp/wp-content/uploads/2014/12/web_09544.jpg http://i1.wp.com/heru3.com/wp/wp-content/uploads/2014/12/web_09544.jpg http://i2.wp.com/heru3.com/wp/wp-content/uploads/2014/12/web_09558.jpg http://i2.wp.com/heru3.com/wp/wp-content/uploads/2014/12/web_09558.jpg http://i0.wp.com/heru3.com/wp/wp-content/uploads/2014/12/web_09565.jpg http://i0.wp.com/heru3.com/wp/wp-content/uploads/2014/12/web_09565.jpg http://i0.wp.com/heru3.com/wp/wp-content/uploads/2014/12/web_09704.jpg http://i0.wp.com/heru3.com/wp/wp-content/uploads/2014/12/web_09704.jpg http://i2.wp.com/heru3.com/wp/wp-content/uploads/2014/12/web_09706.jpg http://i2.wp.com/heru3.com/wp/wp-content/uploads/2014/12/web_09706.jpg http://i2.wp.com/heru3.com/wp/wp-content/uploads/2014/12/web_09601.jpg http://i2.wp.com/heru3.com/wp/wp-content/uploads/2014/12/web_09601.jpg[/code]
                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: