如何从网页中提取所需数据?

wpan110 2012-08-02 09:35:07
我已做了一个程序,输入URL地址,能够从该地址中提取HTML中的数据...例如 我输入百度首页 显示如下:连接到站点http://www.baidu.com

<!doctype html><html><head><meta http-equiv="Content-Type" content="text/html;charset=gb2312"><title>百度一下,你就知道 </title><style>html{overflow-y:auto}body{font:12px arial;text-align:center;background:#fff}body,p,form,ul,li{margin:0;padding:0;list-style:none}body,form,#fm{position:relative}td{text-align:left}img{border:0}a{color:#00c}a:active{color:#f60}#u{color:#999;padding:4px 10px 5px 0;text-align:right}#u a{margin:0 5px}#u .reg{margin:0}#m{width:680px;margin:0 auto;}#nv a,#nv b,.btn,#lk{font-size:14px}#fm{padding-left:90px;text-align:left}input{border:0;padding:0}#nv{height:19px;font-size:16px;margin:0 0 4px;text-align:left;text-indent:117px;}.s_ipt_wr{width:418px;height:30px;display:inline-block;margin-right:5px;background:url(http://s1.bdstatic.com/r/www/cache/aoyun/img/i-1.0.1.png) no-repeat -304px 0;border:1px solid #b6b6b6;border-color:#9a9a9a #cdcdcd #cdcdcd #9a9a9a;vertical-align:top}.s_ipt{width:405px;height:22px;font:16px/22px arial;margin:5px 0 0 7px;background:#fff;outline:none;-webkit-appearance:none}.s_btn{width:95px;height:32px;padding-top:2px\9;font-size:14px;background:#ddd url(http://s1.bdstatic.com/r/www/cache/aoyun/img/i-1.0.1.png);cursor:pointer}.s_btn_h{background-position:-100px 0}.s_btn_wr{width:97px;height:34px;display:inline-block;background:url(http://s1.bdstatic.com/r/www/cache/aoyun/img/i-1.0.1.png) no-repeat -202px 0;*position:relative;z-index:0;vertical-align:top}#lg img{vertical-align:top;margin-bottom:3px}#lk{margin:33px 0}#lk span{font:14px "宋体"}#lm{height:60px}#lh{margin:16px 0 5px;word-spacing:3px}.tools{position:absolute;top:-4px;*top:10px;right:-13px;}#mHolder{width:62px;position:relative;z-index:296;display:none}#mCon{height:18px;line-height:18px;position:absolute;cursor:pointer;padding:0 18px 0 0;background:url(http://s1.bdstatic.com/r/www/img/bg-1.0.0.gif) no-repeat right -134px;background-position:right -136px\9}#mCon span{color:#00c;cursor:default;display:block}#mCon .hw{text-decoration:underline;cursor:pointer}#mMenu a{width:100%;height:100%;display:block;line-height:22px;text-indent:6px;text-decoration:none;filter:none\9}#mMenu,#user ul{box-shadow:1px 1px 2px #ccc;-moz-box-shadow:1px 1px 2px #ccc;-webkit-box-shadow:1px 1px 2px #ccc;filter: progid:DXImageTransform.Microsoft.Shadow(Strength=2, Direction=135, Color="#cccccc")\9;}#mMenu{width:56px;border:1px solid #9b9b9b;list-style:none;position:absolute;right:7px;top:28px;display:none;background:#fff}#mMenu a:hover{background:#ebebeb}#mMenu .ln{height:1px;background:#ebebeb;overflow:hidden;font-size:1px;line-height:1px;margin-top:-1px}#cp,#cp a{color:#77c}#seth{display:none;behavior:url(#default#homepage)}#setf{display:none;}#sekj{margin-left:14px;}</style>
<script type="text/javascript">function h(obj){obj.style.behavior='url(#default#homepage)';var a = obj.setHomePage('http://www.baidu.com/');}</script></head>

<body><div id="u"><a href="http://www.baidu.com/gaoji/preferences.html" name="tj_setting">搜索设置</a>|<a href="https://passport.baidu.com/v2/?login&tpl=mn&u=http%3A%2F%2Fwww.baidu.com%2F" name="tj_login" id="lb" onclick="return false;">登录</a><a href="https://passport.baidu.com/v2/?reg®Type=1&tpl=mn&u=http%3A%2F%2Fwww.baidu.com%2F" target="_blank" name="tj_reg" class="reg">注册</a></div>
<div id="m"><p id="lg"><img src="http://www.baidu.com/img/baidu_sylogo1.gif" width="270" height="129"></p>
<p id="nv"><a href="http://news.baidu.com">新 闻</a> <b>网 页</b> <a href="http://tieba.baidu.com">贴 吧</a> <a href="http://zhidao.baidu.com">知 道</a> <a href="http://mp3.baidu.com">MP3</a> <a href="http://image.baidu.com">图 片</a> <a href="http://video.baidu.com">视 频</a> <a href="http://map.baidu.com">地 图</a></p><div id="fm"><form name="f" action="/s"><span class="s_ipt_wr"><input type="text" name="wd" id="kw" maxlength="100" class="s_ipt"></span><input type="hidden" name="rsv_bp" value="0"><input type="hidden" name="rsv_spt" value="3"><span class="s_btn_wr"><input type="submit" value="百度一下" id="su" class="s_btn" onmousedown="this.className='s_btn s_btn_h'" onmouseout="this.className='s_btn'"></span></form><span class="tools"><span id="mHolder"><div id="mCon"><span>输入法</span></div></span></span><ul id="mMenu"><li><a href="#" name="ime_hw">手写</a></li><li><a href="#" name="ime_py">拼音</a></li><li class="ln"></li><li><a href="#" name="ime_cl">关闭</a></li></ul></div>
<p id="lk"><a href="http://baike.baidu.com">百科</a> <a href="http://wenku.baidu.com">文库</a> <a href="http://www.hao123.com">hao123</a><span> | <a href="/more/">更多>></a></span></p><p id="lm"></p><p><a id="seth" onClick="h(this)" href="http://utility.baidu.com/traf/click.php?id=215&url=http://www.baidu.com" onmousedown="return ns_c({'fm':'behs','tab':'homepage','pos':0})">把百度设为主页</a><a id="setf" href="http://www.baidu.com/cache/sethelp/index.html" onmousedown="return ns_c({'fm':'behs','tab':'favorites','pos':0})" target="_blank">把百度设为主页</a><span id="sekj"><a href="http://www.baidu.com/search/baidukuijie_mp.html" target="_blank" onmousedown="return ns_c({'fm':'behs','tab':'kuaijie','pos':1})">把百度添加到桌面</a></span></p>
<p id="lh"><a href="http://e.baidu.com/?refer=888">加入百度推广</a> | <a href="http://top.baidu.com">搜索风云榜</a> | <a href="http://home.baidu.com">关于百度</a> | <a href="http://ir.baidu.com">About Baidu</a></p><p id="cp">©2012 Baidu <a href="/duty/">使用百度前必读</a> <a href="http://www.miibeian.gov.cn" target="_blank">京ICP证030173号</a> <img src="http://www.baidu.com/cache/global/img/gs.gif"></p></div></body>

<script>var bds={se:{},comm : {ishome : 1,sid : "1289_1328_1262_1227_1323_1186_1280_1284_1318_1332",user : "",username : "",sugHost : "http://suggestion.baidu.com/su",loginAction : []}}</script><script type="text/javascript" src="http://s1.bdstatic.com/r/www/cache/global/js/home-1.2.js"></script><script>var bdUser = null;var w=window,d=document,n=navigator,k=d.f.wd,a=d.getElementById("nv").getElementsByTagName("a"),isIE=n.userAgent.indexOf("MSIE")!=-1&&!window.opera;for(var i=0;i<a.length;i++){a[i].onclick=function(){if(k.value.length>0){var o=this,h=o.href,q=encodeURIComponent(k.value);if(h.indexOf("q=")!=-1){o.href=h.replace(/q=[^&\x24]*/,"q="+q)}else{this.href+="?q="+q}}}};(function(){if(/q=([^&]+)/.test(location.search)){k.value=decodeURIComponent(RegExp["\x241"])}})();if(n.cookieEnabled&&!/sug?=0/.test(d.cookie)){bds.se.sug();};function addEV(o, e, f){if(w.attachEvent){o.attachEvent("on" + e, f);}else if(w.addEventListener){ o.addEventListener(e, f, false);}}function G(id){return d.getElementById(id);}function ns_c(q){var p = encodeURIComponent(window.document.location.href), sQ = '', sV = '', mu='', img = window["BD_PS_C" + (new Date()).getTime()] = new Image();for (v in q) {sV = q[v];sQ += v + "=" + sV + "&";} mu= "&mu=" + p ;img.src = "http://nsclick.baidu.com/v.gif?pid=201&pj=www&rsv_sid=1289_1328_1262_1227_1323_1186_1280_1284_1318_1332&" + sQ + "path="+p+"&t="+new Date().getTime();return true;}if(/\bbdime=[12]/.test(d.cookie)){document.write('<script src=http://s1.bdstatic.com/r/www/cache/ime/js/openime-1.0.0.js><\/script>');}(function(){var u = G("u").getElementsByTagName("a"), nv = G("nv").getElementsByTagName("a"), lk = G("lk").getElementsByTagName("a"), un = "";var tj_nv = ["news","tieba","zhidao","mp3","img","video","map"];var tj_lk = ["baike","wenku","hao123","more"];un = bds.comm.user == "" ? "" : bds.comm.user;function _addTJ(obj){addEV(obj, "mousedown", function(e){var e = e || window.event;var target = e.target || e.srcElement;ns_c({'fm':'behs','tab':target.name||'tj_user','un':encodeURIComponent(un)});});}for(var i = 0; i < u.length; i++){_addTJ(u[i]);}for(var i = 0; i < nv.length; i++){nv[i].name = 'tj_' + tj_nv[i];_addTJ(nv[i]);}for(var i = 0; i < lk.length; i++){lk[i].name = 'tj_' + tj_lk[i];_addTJ(lk[i]);}})();addEV(w,"load",function(){k.focus()});w.onunload=function(){};</script><script type="text/javascript" src="http://s1.bdstatic.com/r/www/cache/global/js/tangram-1.3.4c1.0.js"></script><script type="text/javascript" src="http://s1.bdstatic.com/r/www/cache/user/js/u-1.3.1.js"></script><script type="text/javascript" src="http://s1.bdstatic.com/r/www/cache/aoyun/js/aoyun-1.2.js"></script>



</html>



<!--4393f2d199fc71b8-->


请问我该如何从中提取出那些文字 ? 急求 ...谢谢了 !
...全文
921 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
IVERS0N 2012-08-08
  • 打赏
  • 举报
回复
大规模的用正则表达式
RLib 2012-08-08
  • 打赏
  • 举报
回复
有規則 自己提取
較無規則 正則表達式
RLib 2012-08-08
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 的回复:]

XML 解析 用TinyXML库 。 转换为CString 仅限于字符串少的情况下。 用正则表达式。
[/Quote]

TinyXML不能直接用來解析HTML,數據不完整.官方明確指出
ranshon 2012-08-08
  • 打赏
  • 举报
回复
XML 解析 用TinyXML库 。 转换为CString 仅限于字符串少的情况下。 用正则表达式。
toucheme 2012-08-08
  • 打赏
  • 举报
回复
如果对效率要求不高,可以用让ie来提取

BOOL GetContextFromHtml(LPSTR pszHtmlBuffer,CString&sText)
{
IHTMLDocument2 *pDoc = NULL;
_bstr_t btHtml,vtContext;
btHtml = pszHtmlBuffer;
//vtHtml = pszHtmlBuffer;

CoInitialize(NULL);

CoCreateInstance(CLSID_HTMLDocument, NULL, CLSCTX_INPROC_SERVER, IID_IHTMLDocument2, (LPVOID*)&pDoc);
if(pDoc)
{
IPersistStreamInit *pPersist = NULL;
pDoc-> QueryInterface(IID_IPersistStreamInit, (LPVOID*)&pPersist);
if(pPersist)
{
IMarkupServices *pMS=NULL;

pPersist->InitNew();
pPersist->Release();

pDoc->QueryInterface(IID_IMarkupServices, (LPVOID*)&pMS);

if(pMS)
{
IMarkupContainer *pMC = NULL;
IMarkupPointer *pMkStart = NULL;
IMarkupPointer *pMkFinish = NULL;
pMS->CreateMarkupPointer(&pMkStart);
pMS->CreateMarkupPointer(&pMkFinish);
pMS->ParseString(btHtml, 0, &pMC, pMkStart, pMkFinish);

if(pMC)
{
IHTMLDocument2 *pNewDoc = NULL;
pMC-> QueryInterface(IID_IHTMLDocument, (LPVOID*)&pNewDoc);
if(pNewDoc)
{
// do anything with pNewDoc, in this case
// get the body innerText.
IHTMLElement *pBody;
pNewDoc->get_body(&pBody);
if(pBody)
{
BSTR strText;
pBody->get_innerText(&strText);
pBody->Release();
vtContext.Assign(strText);
SysFreeString(strText);
}
pNewDoc->Release();
}
pMC->Release();
}

if(pMkStart)
pMkStart->Release();

if(pMkFinish)
pMkFinish->Release();

pMS->Release();
}
}
pDoc->Release();
}
sText = (char*)vtContext;
CoUninitialize();

return TRUE;
}
zhouzhipen 2012-08-07
  • 打赏
  • 举报
回复
正则表达式,就是为了处理这类东西的,如果有写过PHP程序,就不会提这种问题了。
Google搜索“正则表达式去除HTML标记”。
whucv 2012-08-07
  • 打赏
  • 举报
回复
http://blog.csdn.net/whucv/article/details/7837434
看我这篇博客,写的怎样获取正文
http://blog.csdn.net/whucv/article/details/7837434
just_soso888 2012-08-03
  • 打赏
  • 举报
回复
可以自己写个字符串筛选,如5楼所说的那样。

还可以用别人封装的正则表达式的类,我这里就有一份
Gloveing 2012-08-03
  • 打赏
  • 举报
回复
依次判断字符的ascii码,是负数的就是汉字
whucv 2012-08-03
  • 打赏
  • 举报
回复
ActiveX空见MSIE自带的函数就可以根据标记提取内容,比如tag <html><body><script>等,获得标记集合然后从中提取内容。
或者可以利用正则表达式
在vc中使用regex的一个简便方法:
1. 生成regex.tlb
1.1 以资源方式打开 c:\windows\system32\vbscript.dll,将第二个typelib导出为 regex.tlb
1.2 vc, tools --> ole/com object viewer,找到 Type Libraraies --> Microsoft VBScript Regular Expression 5.5, 右键 View..,然后在打开的 ITypeLib Viewer 中保存成 regex.idl,再用 midl 工具生成 regex.tlb。这个方法能找到很多其他的 tlb

2. 在vc中导入 regex.tlb
#import "regex.tlb" rename_namespace("RegEx")

参考文章:www.codeproject.com/KB/string/use_regular_expression_in_your_program.aspx


长尾巴的悟空 2012-08-03
  • 打赏
  • 举报
回复
那就判断是否是字母,不是字母与数字的,当然就是汉字了。
wpan110 2012-08-03
  • 打赏
  • 举报
回复
嗯嗯 就是提取汉字 ....只要学会提取汉字,我就可以提取其它了 .....
xiaohuh421 2012-08-03
  • 打赏
  • 举报
回复
XML相关类可以做.
也可以使用MS提供的关于网页操作的COM类.
hztj2005 2012-08-03
  • 打赏
  • 举报
回复
这个叫网页解析,到网上搜,如果找到好的代码,告知一下。
Gloveing 2012-08-02
  • 打赏
  • 举报
回复
提取汉字?
wpan110 2012-08-02
  • 打赏
  • 举报
回复
麻烦各位帮帮 ....

3,055

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC HTML/XML
社区管理员
  • HTML/XML社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧