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

C++利用BOOST和Wget提取百度图片实例

2012-04-28 19:50 211 查看
本文转自.http://www.郭华.com/?p=235 转载请注明出处
作者: gh0716 / 2012年04月28日

C++利用BOOST和Wget提取百度图片实例。

只是作为一个简单的模型,没有其他目的。

由于临时写的,文件名和路径都很乱。抛砖引玉的作用。可以修改,做成其他批量工具。

转载、复制请注明出处http://www.xn--xkrx74l.com/?p=235

效果如下:



提取效果图:



源代码:

/*
 *作者:gh0716
 *邮箱:gh0716@163.com
 *原理:boost分析网页
 *网址:http://www.xn--xkrx74l.com/?p=235
 *说明:需要配置boost和放置wget到exe目录,或者改写下载方式
 *时间:2012年4月28日19:42:10
 *目的:抛砖引玉
*/
#include<iostream>
#include <boost/regex.hpp>
#include <fstream>
#include <UrlMon.h>
#pragma comment(lib, "urlmon.lib")

using namespace std;   
using namespace boost;

int main()   
{   
    regex reg("objURL\\\":\\\"([^\\\'\\\"\\s?]+\\.jpg)\\s*", boost::regex::perl|boost::regex::icase);     
    smatch m;     
	
	HRESULT hr =URLDownloadToFile(0,"http://image.baidu.com/i?ct=201326592&cl=2&nc=1&lm=-1&st=-1&tn=baiduimage&istype=2&fm=index&pv=&z=0&word=%C3%C0%CD%BC&s=0","index.html", 0,NULL);
	if(hr==S_OK)
	{
		fstream file2("index.lst",ios::out|ios::app);
		fstream file("index.html",ios::in);     
		string k;   
		string s;   
		while(!file.eof())   
		{   
			file >> k;   
			s += k;   
		}   
		string::const_iterator it = s.begin();     
		string::const_iterator end = s.end();     
		int n = 0;   
	

		while (boost::regex_search(it,end,m,reg))    
		{   
			n ++;   
			cout << m[1] << endl; 			
			file2<<m[1]<<endl;
			it = m[1].second;   
		}   
		 
		cout <<"共匹配到 "<< n <<"个链接"<< endl;  
		file.close();
		file2.close();

		ShellExecute(NULL,"open","wget.exe" ,"-b -i index.lst -c",NULL,SW_HIDE);
		cout<<"OK"<<endl;

	}
    return 0;   
}


说明,需要配置boost和复制wget等部分文件过来。
需要源码的可以留下邮箱。


本文转自.http://www.郭华.com/?p=235 转载请注明出处。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: