📄 htmlparser.cpp
字号:
{
URL_LEVEL url_level;
url_level.m_url=strText;
url_level.level=level;
m_url_level.Add(url_level);
}
}
}//if(strText=="href")
else if(strText=="src")
{
strText=m_arrparapair[i].GetValue();
if(WantFileType(strText))
{
MakeQualifiedUrl(strText,level);
if(level>0)
{
URL_LEVEL url_level;
url_level.m_url=strText;
url_level.level=level;
m_url_level.Add(url_level);
}
}
}
}//end for
} //处理图片链接
else if(m_strTag=="img" || m_strTag=="embed" || m_strTag=="body")
{
temp.Empty();
bool accept=true;
level=m_nLevel;
for(int i=0;i<m_arrparapair.GetSize();i++)
{
strText=m_arrparapair[i].GetPara();
strText.MakeLower();
if(strText=="src")
{
strText=m_arrparapair[i].GetValue();
strText=strText.SpanExcluding("?");
strText.TrimLeft();
if(strText.Left(1)=="#")
break;
ii=WantImageType(strText);
//我们不感兴趣的地方
if((ii==-1)||(!(m_image_type&ii)))
accept=false;
temp=m_arrparapair[i].GetValue();
}else if(strText=="width")
{
strText=m_arrparapair[i].GetValue();
for(ii=0;ii<strText.GetLength();ii++)
if(!isdigit(strText[ii]))
break;
if(ii==strText.GetLength())
{
ii=atoi(strText);
if(ii>m_max_width||ii<m_min_width)
accept=false;
}
//break;
}else if(strText=="height")
{
strText=m_arrparapair[i].GetValue();
for(ii=0;ii<strText.GetLength();ii++)
if(!isdigit(strText[ii]))
break;
if(ii==strText.GetLength())
{
ii=atoi(strText);
if(ii>m_max_length||ii<m_min_length)
accept=false;
}
}else if(strText=="onclick")
{
//本部分对应的特定的网站为www.coldwatercreek.com
strText=m_arrparapair[i].GetValue();
CString zhang=strText.Left(25);
zhang.MakeLower();
if(m_site_maxlevel-m_exclusive_level>=level)
if(zhang=="javascript:showdetailview")
{
zhang=strText.SpanExcluding(",");
int i;
i=zhang.Find("(");
if((i!=-1)&&(m_site_maxlevel-m_exclusive_level>=level))
{
zhang=zhang.Mid(i+1);
for(i=0;i<zhang.GetLength();i++)
if((zhang[i]=='\'')||(zhang[i]=='"'))
zhang.SetAt(i,' ');
zhang.TrimLeft();
zhang.TrimRight();
CString xx;
xx="../html/productzoomPopUp.html?src=winMain&ImageFileName="+zhang;
MakeQualifiedUrl(xx,level);
m_arrStrImage.Add(xx);
MakeQualifiedUrl(zhang,level);
m_arrStrImage.Add(zhang);
}
}
}
}// for(int i=0;i<m_arrparapair.GetSize();i++)
if(!temp.IsEmpty()&&accept)
{
if(m_site_maxlevel-m_exclusive_level>=level)
{
if(temp.Left(1)=="\\")
temp.SetAt(0,' ');
if(temp.Right(1)=="\\")
temp.SetAt(temp.GetLength()-1,' ');
temp.TrimLeft();
temp.TrimRight();
MakeQualifiedUrl(temp,level);
m_arrStrImage.Add(temp);
}
}
}//if(m_strTag=="img" || m_strTag=="embed" || m_strTag=="body")
else if(m_strTag="param")
{
//在本部分处理flash文件
if(m_image_type&64)//判断是否需要下载扩展名称为swf的文件
{
for(int i=0;i<m_arrparapair.GetSize();i++)
{
strText=m_arrparapair[i].GetPara();
strText.MakeLower();
if(strText=="value")
{
int x;
strText=m_arrparapair[i].GetValue();
strText.MakeLower();
x=strText.Find(".swf");
if(x!=-1)
{
strText=m_arrparapair[i].GetValue().Left(x+4);
for(int ll=0;ll<strText.GetLength();ll++) //去掉字符串中的单双引号
if((strText[ll]=='"')||(strText[ll]=='\''))
strText.SetAt(ll,' ');
strText.TrimLeft();
strText.TrimRight();
MakeQualifiedUrl(strText,level);
m_arrStrImage.Add(strText);
}
}
}
}
}
return true;
}
//对应于以前的GetOptions(const char *pBuffer, int &nIndex, int nMaxLen)
//现在分析的Buffer的内容为一个<... >内的内容nMaxLen的为>后字符的第一个索引
void CHtmlParser::GetParaPair(const char *pBuffer, int &nIndex, int nMaxLen)
{
CString strPara;
int nStart;
//重复遍历buffer 直到完成
while(nIndex < nMaxLen)
{
nStart=nIndex;
//找到第一个=号
strPara=GetString("=",pBuffer,nIndex,nMaxLen);
nIndex++;
if(!strPara.IsEmpty())
{
CParaPair opt;
nStart=nIndex;
strPara.TrimLeft();
strPara.TrimRight();
strPara.MakeLower();
opt.SetPara(strPara);
//跳过开头的空格
while(nIndex<nMaxLen && pBuffer[nIndex]==' ')
nIndex++;
//分析param的值 或则结束此tag
int k;
if(pBuffer[nIndex]=='"')
{
k=nIndex+1;
FindToken("\"",pBuffer,k,nMaxLen);
//nIndex++;
}
else if(pBuffer[nIndex]=='\'')
{
k=nIndex+1;
FindToken("'",pBuffer,k,nMaxLen);
//nIndex++;
}
else
{
k=nIndex;
FindToken(" >",pBuffer,k,nMaxLen);
//if(pBuffer[nIndex]=='>')
//pBuffer[nIndex]=' ';// --nIndex;
}
nIndex=k;
nIndex++;
//以后要分析
strPara.Empty();
strncpy(strPara.GetBuffer(nIndex-nStart),pBuffer+nStart,nIndex-nStart);
strPara.ReleaseBuffer();
//将来看是否有必要这样做?
int nLen=strPara.GetLength();
for(int i=0;i<nLen;i++){
if((strPara[i]=='"')||(strPara[i]=='\''||strPara[i]=='>'))
strPara.SetAt(i,' ');
}
strPara.TrimLeft();
strPara.TrimRight();
opt.SetValue(strPara);
m_arrparapair.Add(opt);
}
else break;
}
}
bool CHtmlParser::WantFileType(CString &strText)
{
CString strExt=CInet::SplitFileName(strText,CInet::EXT);
strExt.MakeLower();
strExt=strExt.SpanExcluding("#");
/*
if(strExt==".com" ||strExt.IsEmpty())
return true;
for(int i=0;i<g_nValidFiles;i++)
if(strExt==g_aszValidFiles[i])
return true;
return false;
*/
//现在采用排除法排除不需要的文件扩展名
for(int i=0;i<g_nValidFiles;i++)
if(strExt==g_aszValidFiles[i])
return false;
return true;
}
int CHtmlParser::WantImageType(CString &strText)
{
int k=-1;
CString strExt=CInet::SplitFileName(strText,CInet::EXT);
strExt.MakeLower();
strText=strText.SpanExcluding("#");
for(int i=0;i<g_nValidImages;i++)
if(strExt==g_aszValidImages[i])
{
k= i;
break;
}
if(k!=-1)
k=1<<i;
return k;
}
//向分析器类的实例传递从输入的url中解析出的m_strServer和m_strObject变量
void CHtmlParser::SetPageURL(CString& strServer,CString& strObject)
{
m_strServer=strServer;
m_strObject=strObject;
return;
}
//本函数的功能为使用strText构造一个完整的url
void CHtmlParser::MakeQualifiedUrl(CString &strText,int& level)
{
CString strServer,strObject,strUser,strPassword;
char szOut[1000];
INTERNET_PORT nPort;
DWORD dwServiceType;
unsigned long ulLen=sizeof(szOut);
//下面开始构造strText的完整的url路径
if(!AfxParseURLEx(strText,dwServiceType,strServer,strObject,nPort,
strUser,strPassword,ICU_NO_ENCODE))
{
//现在说明strText是本网站的链接或则是一个绝对的目录或则是一个相对的目录
if (strText.Left(1)=="/")
{
//现在说明strText是一个绝对的目录
strServer="http://"+m_strServer;
::InternetCombineUrl(strServer,strText,szOut,&ulLen,ICU_NO_ENCODE);
strText=szOut;
}
else
{
//现在说明strText是一个相对的目录既和m_parenturl指定目录相对的。
strText=m_parenturl+strText;
}
//现在strText已经是一个完整的url
--level;
}else
{
//现在说明strText是一个完整的url路径既它本身可以个用来链接的串
if(m_strServer==strServer)
level--;
else
{
level--;
level=min(level,m_unlike_level);
}
}
int i=strText.Find("&");
if(i!=-1)
{
CString xx;
xx=strText.Left(i+1);
xx=xx+strText.Mid(i+5);
strText=xx;
}
return;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -