好友
阅读权限20
听众
最后登录1970-1-1
|
本帖最后由 liye1320 于 2015-3-10 15:30 编辑
百度,必应,搜狗,360图片搜索下载工具 ImageDownloaderV1.0
本程序使用VS2008编译,原理为利用CInternetSession,CHttpConnection,CHttpFile等MFC类发Get包,获取包数据后,利用正则表达式解析出图片的原始地址,进行下载。下面有部分源码说明。程序无壳。
界面如图:
百度云盘下载:http://pan.baidu.com/s/1i3sxGCd
论坛下载:
ImageDownloaderV1.0.zip
(147.97 KB, 下载次数: 265)
如有任何BUG或者建议,欢迎大家跟帖说明。
百度部分源码示例:
1.数据包下载线程
[C++] 纯文本查看 复制代码 DWORD WINAPI BaiduDownloadThread(LPVOID lpParam)
{
UINT32 result = ERROR_SUCCESS;
CImageDownloaderDlg* pDlg = (CImageDownloaderDlg*)(lpParam);
pDlg->m_bIfRunning_Baidu = true;
CHttpConnection* pHttpConn = NULL;
CInternetSession Session(_T("BaiduSession"),
0,
INTERNET_OPEN_TYPE_PRECONFIG,
NULL,
NULL,
INTERNET_FLAG_DONT_CACHE); //设置不读缓冲
try
{
CString strSaveDir;
strSaveDir.Format(_T("%s%s\\"), pDlg->m_strSaveFolderPath, DIR_NAME_BAIDU);
MakeSureDirectoryPathExists(CStrW2CStrA(strSaveDir).GetBuffer(0));
CString strKeyWordUTF8Hex;
String2UTF8Hex(pDlg->m_strKeyWord, strKeyWordUTF8Hex);
DWORD dwLen = 0;
byte* pRespon = NULL;
DWORD dwPageCount = 0;
CString strHttpServer = _T("image.baidu.com");
CString strURL;
strURL.Format(_T("/i?tn=baiduimage&ipn=r&ct=201326592&cl=2&lm=-1&st=-1&fm=result\
&fr=&sf=1&fmq=1425437734117_R&pv=&ic=0&nc=1&z=&se=1&showtab=0\
&fb=0&width=&height=&face=0&istype=2\
&ie=utf-8&word=%s"), strKeyWordUTF8Hex);
pHttpConn = Session.GetHttpConnection(strHttpServer);
result = GetHttpRequest(Session, pHttpConn, strURL, &pRespon, &dwLen);
if (ERROR_SUCCESS != result)
{
//throw result;
}
result = pDlg->AnalyseBaiduData(pRespon, dwLen, strSaveDir, ++dwPageCount);
if (pRespon)
{
delete pRespon;
pRespon = NULL;
}
if (result == ERROR_DOWNLOAD_FINISHED)
{
throw result;
}
if (result == ERROR_NOT_ANY_RESULTS)
{
pDlg->AddLog(_T("百度图片无任何搜索结果..."));
throw result;
}
while(pDlg->m_iThreadCMD == THREAD_STATUS_RUNING) //一页页的下载
{
//GET /i?tn=resultjson_com&ipn=rj&ct=201326592&cl=2&lm=-1&st=-1&fm=result&fr=&sf=1&fmq=1425437734117_R&pv=&ic=0&nc=1&z=&se=1&showtab=0&fb=0&width=&height=&face=0&istype=2&ie=gbk&word=%CA%F7%DD%AE%C5%C9&oe=utf-8&rn=60&pn=60&1189663543109.5625&260800525383.43848
strURL.Format(_T("/i?tn=resultjson_com&ipn=rj&ct=201326592&cl=2&lm=-1&st=-1\
&fm=result&fr=&sf=1&fmq=1425437734117_R&pv=&ic=0&nc=1&z=&se=1\
&showtab=0&fb=0&width=&height=&face=0&istype=2&ie=utf-8&word=%s\
&oe=utf-8&rn=%d&pn=%d"),
strKeyWordUTF8Hex, PRE_PAGE_COUNT_BAIDU, (dwPageCount)*PRE_PAGE_COUNT_BAIDU);
result = GetHttpRequest(Session, pHttpConn, strURL, &pRespon, &dwLen);
if (ERROR_SUCCESS != result)
{
//throw result;
}
result = pDlg->AnalyseBaiduData(pRespon, dwLen, strSaveDir, ++dwPageCount);
if (pRespon)
{
delete pRespon;
pRespon = NULL;
}
if (result == ERROR_DOWNLOAD_FINISHED)
{
break;
}
}
}
catch (UINT32)
{
}
if (pHttpConn)
{
pHttpConn->Close();
delete pHttpConn;
pHttpConn = NULL;
}
Session.Close();
pDlg->m_bIfRunning_Baidu = false;
pDlg->AddLog(_T("百度图片下载结束..."));
if (pDlg->IfAllThreadExit() && (pDlg->m_iThreadCMD == THREAD_STATUS_RUNING))
{
pDlg->StopDowndThread();
}
return result;
}
2.数据包解析部分
[C++] 纯文本查看 复制代码 UINT32 CImageDownloaderDlg::AnalyseBaiduData(byte* pRespon, DWORD dwLen, CString strSaveDir, DWORD& dwPageCount)
{
UINT32 result = ERROR_SUCCESS;
try
{
char* pResponChar = NULL;
pResponChar = new char[dwLen*2];
ZeroMemory(pResponChar, dwLen*2);
UINT32 uintLen = 0;
result = UTF8ToChar((char*)pRespon, dwLen, pResponChar, &uintLen);
if (ERROR_SUCCESS != result)
{
throw result;
}
std::string str = pResponChar;
if (pResponChar)
{
delete pResponChar;
pResponChar = NULL;
}
//"objURL":"ippr_z2C$qAzdH3FAzdH3Frl_z&e3Bqit42_z&e3Bv54AzdH3Fpa80mbm9mk8bnbduuum_z&e3B3r2"
boost::regex expression("\"objURL\":\"(\\S*?)\""); //\S 匹配任意不是空白符的字符
boost::smatch mat;
string::const_iterator start = str.begin();
string::const_iterator end = str.end();
if (!regex_search(start, end, mat, expression))
{
result = ERROR_NOT_ANY_RESULTS;
throw result;
}
while( regex_search(start, end, mat, expression) && (m_iThreadCMD == THREAD_STATUS_RUNING))
{
string msg(mat[1].first, mat[1].second); //[0]代表整个匹配字符串, [1]代表第一个()中的字符串
//解密图片 url
CString strDURL(msg.c_str());
DecryptURL(strDURL);
FormateURL(strDURL);
//下载图片 url
int is = strDURL.ReverseFind('/');
is++;
CString strPicName = strDURL.Mid(is, strDURL.GetLength()-is);
CString strLocal;
strLocal.Format(_T("%s%d_%s"), strSaveDir, ++m_dwTotalNum_Baidu, strPicName);
CString strResult;
bool bRes = DownloadUrlFile(strDURL.GetBuffer(0), strLocal.GetBuffer(0));
if (bRes)
{
strResult = _T("成功!");
}
else
{
m_dwTotalFailedNum_Baidu++;
strResult.Format(_T("失败! 图片URL为:%s"), strDURL);
}
CString strLog;
strLog.Format(_T("百度下载“%s”第%d页 第%d张...%s"), m_strKeyWord, dwPageCount, m_dwTotalNum_Baidu, strResult);
AddLog(strLog);
start = mat[1].second;
}
}
catch (UINT32)
{
}
return result;
}
|
免费评分
-
查看全部评分
|