让我的C++程序直接阅读网页(3) 读取超链接

王朝c/c++·作者佚名  2006-02-01
窄屏简体版  字體: |||超大  

3. 读取超链接函数:

getHTMLink

功能:

读取超链接中的URI, 即 href

原型:

void

getHTMLLink( string & uri,

const string & htmltext,

UInt32 index,

const string & name = “” )

参数:

uri [out]

检索到的URI

htmltext [in]

需要检索的HTML文本, htmltext应经过规范化处理

index [in]

需要检索的hyperlink序号,即从htmltext起始的第index个

name [in]

需要检索的hyperlink名称,缺省默认为标签 "a"

说明:

getHTMLLink 既能通过a 标签检索,也能通过形式如 <a>name</a> 中的 name 来检索

例如:

有连接 <a href=”link-uri”>新闻</a>

设置参数 name = “新闻”, 就能检索到 uri = “link-uri”

此函数尚不支持从非 href 中检索URI, 如 <img src=”link-uri” />

代码:

void

getHTMLLink( string & uri,

const string & htmltext,

UInt32 index,

const string & name )

{

uri.resize( 0 );

if ( htmltext.empty() )

return;

string start_tag( "" );

if ( name.empty() )

start_tag = "<a";

else

start_tag = name;

UInt32 e_pos = string::npos;

UInt32 s_pos = string::npos;

UInt32 tpos = 0;

// find the start tag

for ( UInt32 i = 1; i <= index; i++ )

{

tpos = htmltext.find( start_tag.c_str(), tpos );

if ( tpos != string::npos )

tpos += start_tag.size() ;

else

i = index + 1;

}

if ( tpos == string::npos )

return;

s_pos = tpos - start_tag.size();

if ( !name.empty() )

{

start_tag = "<a";

tpos = htmltext.rfind( start_tag.c_str(), tpos );

if ( tpos == string::npos )

return;

if ( htmltext.rfind( "</a>", s_pos ) > tpos )

return;

}

tpos = htmltext.find( "href=", tpos );

if ( tpos == string::npos )

return;

tpos += 5;

if ( htmltext[tpos] == '"' )

++tpos;

s_pos = tpos;

e_pos = htmltext.find( '"', tpos );

if ( e_pos == string::npos )

{

e_pos = htmltext.find( '>', tpos );

if ( e_pos == string::npos )

return;

}

char ch;

for ( i = s_pos; i < e_pos; ++i )

{

ch = htmltext[i];

if ( ch != '\t'

&& ch != '\n'

&& ch != '\v'

&& ch != '\f'

&& ch != '\r'

&& ch != 0x20 )

uri.append( 1, ch );

}

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航