标 题: C++ 提取网页内容系列
作 者: itdef
链 接: http://www.cnblogs.com/itdef/p/4171179.html
欢迎转帖 请保持文本完整并注明出处
首先分析网页就要下载网页内容 这里给出了两种方案
一种是使用MFC自带函数
代码如下:
int GetHttpFileData(CString strUrl,char* szDownloadHtmFileName)
{
CInternetSession Session("Internet Explorer", 0);
CHttpFile *pHttpFile = NULL;
CString strData;
CString strClip;
int iRet = -1;
if(szDownloadHtmFileName == NULL)
{
cerr << "DownloadHtmFileName is NULL" << endl;
Session.Close();
return iRet;
}
ofstream of(szDownloadHtmFileName);
if (of.bad())
{
cerr << "of create file error" << endl;
Session.Close();
return iRet;
}
try
{
pHttpFile = (CHttpFile*)Session.OpenURL(strUrl);
while ( pHttpFile->ReadString(strClip) )
{
of << strClip;
}
}catch(CInternetException* pEx)
{
TCHAR pszError[64];
pEx->GetErrorMessage(pszError, 64);
cerr << __FUNCTION__ << pszError << endl;
goto GetHttpFileData_EXIT;
}
iRet = 0;
GetHttpFileData_EXIT:
Session.Close();
of.close();
return iRet;
}
这里我将下载内容写入了一个文件存入硬盘。另外还需要注意的是 网页文件下载的格式可能是宽字节 使用UTF8格式,这里需要将其转换为GBK多字节。
int UTF8Str2GBK(const string& strUTF8,string& strGBK)
{
int i = MultiByteToWideChar(CP_UTF8, 0, strUTF8.c_str(), -1, NULL, 0);
WCHAR *wsz = NULL;
TCHAR *tsz = NULL;
int iRet = -1;
wsz = new WCHAR[i+1];
if( NULL == wsz)
{
goto UTF8Str2GBK_EXIT;
}
MultiByteToWideChar(CP_UTF8, 0, strUTF8.c_str(), -1, wsz, i);
i = WideCharToMultiByte(CP_ACP, 0, wsz, -1, NULL, 0, NULL, NULL);
tsz = new TCHAR[i+1];
if( NULL == tsz)
{
goto UTF8Str2GBK_EXIT;
}
WideCharToMultiByte(CP_ACP, 0, wsz, -1, tsz, i, NULL, NULL);
strGBK = string(tsz);
iRet = 0;
UTF8Str2GBK_EXIT:
delete []wsz;
delete []tsz;
return iRet;
}
全部代码见 http://www.oschina.net/code/snippet_614253_43732
效果图见 http://www.cnblogs.com/itdef/p/4081963.html