小弟做了一个小程序,要提取QQ微博信息放到一个textbox里,用的是webbrowser控件,网页的源代码已经下下来了,就是不知道该怎么提取了,以下是登录后的QQ微博部分网页源码:
<ul id="talkList" class="LC"><li id="69539117636491" rel="1312962788" > <div class="userPic"><a href="http://t.qq.com/szw520yun" title="孙志炜(@szw520yun)"><img src="http://mat1.gtimg.com/www/mb/images/head_50.jpg"/></a></div><div class="msgBox"><div class="userName" rel="szw520yun"><strong><a href="http://t.qq.com/szw520yun" title="孙志炜(@szw520yun)">孙志炜</a><b class="ico_phone" title="手机微博"></b>:</strong></div><div class="msgCnt">提取网页文字咋这么男</div><div class="pubInfo"> <span class="left"> <a class="time" target="_blank" href="http://t.qq.com/p/t/69539117636491" from="2">昨天 15:53</a> <a href="http://t.qq.com/client.php?t=sms" class="f" target="_blank">来自短信</a> </span><div class="funBox"><a href="#" class="relay" num="0">转播</a><span>|</span><a href="/p/t/69539117636491" class="comt" num="0">评论</a><span>|</span><div class="mFun"><a href="#">更多<em class="btn_ldrop"></em></a><div class="mFunDrop"><b></b><b class="mask"></b><p><a href="#" class="delBtn">删除</a></p><p><a href="#" class="fav">收藏</a></p><div class="shareBtn"><p><a href="#">分享</a></p></div><p><a href="http://t.qq.com/p/t/69539117636491" class="detail" target="_blank">详情</a></p></div></div></div></div> </div></li><li id="19142128390414" rel="1312962369" > <div class="userPic"><a href="http://t.qq.com/szw520yun" title="孙志炜(@szw520yun)"><img src="http://mat1.gtimg.com/www/mb/images/head_50.jpg"/></a></div><div class="msgBox"><div class="userName" rel="szw520yun"><strong><a href="http://t.qq.com/szw520yun" title="孙志炜(@szw520yun)">孙志炜</a>:</strong></div><div class="msgCnt">提取QQ微博信息</div><div class="pubInfo"> <span class="left"> <a class="time" target="_blank" href="http://t.qq.com/p/t/19142128390414" from="3">昨天 15:46</a> <a href="http://t.qq.com" class="f" target="_blank">来自腾讯微博</a> </span><div class="funBox"><a href="#" class="relay" num="0">转播</a><span>|</span><a href="/p/t/19142128390414" class="comt" num="0">评论</a><span>|</span><div class="mFun"><a href="#">更多<em class="btn_ldrop"></em></a><div class="mFunDrop"><b></b><b class="mask"></b><p><a href="#" class="delBtn">删除</a></p><p><a href="#" class="fav">收藏</a></p><div class="shareBtn"><p><a href="#">分享</a></p></div><p><a href="http://t.qq.com/p/t/19142128390414" class="detail" target="_blank">详情</a></p></div></div></div></div> </div></li><li id="70539117104187" rel="1312960577" > <div class="userPic"><a href="http://t.qq.com/szw520yun" title="孙志炜(@szw520yun)"><img src="http://mat1.gtimg.com/www/mb/images/head_50.jpg"/></a></div><div class="msgBox"><div class="userName" rel="szw520yun"><strong><a href="http://t.qq.com/szw520yun" title="孙志炜(@szw520yun)">孙志炜</a>:</strong></div><div class="msgCnt">测试第二下2</div><div class="pubInfo"> <span class="left"> <a class="time" target="_blank" href="http://t.qq.com/p/t/70539117104187" from="3">昨天
我想在里面提取"提取网页文字咋这么男",请高手们帮帮忙,怎么提取出来,正则我老是写错。我主要是提取手机微博发表的信息,也就是说这个信息是变化的,每次我用手机发表后后面总会跟着一个“刚刚”,如果过上几分钟,“刚刚”就会变成“几分钟之前”发表了什么,就像这样:
title="孙志炜(@szw520yun)">孙志炜</a>:</strong></div><div class="msgCnt">提取好难过啊</div><div class="pubInfo"> <span class="left"> <a class="time" target="_blank" href="http://t.qq.com/p/t/50553073201528" from="3">刚刚</a> <a href="http://t.qq.com" class="f" target="_blank">来自腾讯微博</a> ,能不能每次提取时判断是不是“刚刚”就提取后面有“刚刚”的文字内容呢???
<ul id="talkList" class="LC"><li id="69539117636491" rel="1312962788" > <div class="userPic"><a href="http://t.qq.com/szw520yun" title="孙志炜(@szw520yun)"><img src="http://mat1.gtimg.com/www/mb/images/head_50.jpg"/></a></div><div class="msgBox"><div class="userName" rel="szw520yun"><strong><a href="http://t.qq.com/szw520yun" title="孙志炜(@szw520yun)">孙志炜</a><b class="ico_phone" title="手机微博"></b>:</strong></div><div class="msgCnt">提取网页文字咋这么男</div><div class="pubInfo"> <span class="left"> <a class="time" target="_blank" href="http://t.qq.com/p/t/69539117636491" from="2">昨天 15:53</a> <a href="http://t.qq.com/client.php?t=sms" class="f" target="_blank">来自短信</a> </span><div class="funBox"><a href="#" class="relay" num="0">转播</a><span>|</span><a href="/p/t/69539117636491" class="comt" num="0">评论</a><span>|</span><div class="mFun"><a href="#">更多<em class="btn_ldrop"></em></a><div class="mFunDrop"><b></b><b class="mask"></b><p><a href="#" class="delBtn">删除</a></p><p><a href="#" class="fav">收藏</a></p><div class="shareBtn"><p><a href="#">分享</a></p></div><p><a href="http://t.qq.com/p/t/69539117636491" class="detail" target="_blank">详情</a></p></div></div></div></div> </div></li><li id="19142128390414" rel="1312962369" > <div class="userPic"><a href="http://t.qq.com/szw520yun" title="孙志炜(@szw520yun)"><img src="http://mat1.gtimg.com/www/mb/images/head_50.jpg"/></a></div><div class="msgBox"><div class="userName" rel="szw520yun"><strong><a href="http://t.qq.com/szw520yun" title="孙志炜(@szw520yun)">孙志炜</a>:</strong></div><div class="msgCnt">提取QQ微博信息</div><div class="pubInfo"> <span class="left"> <a class="time" target="_blank" href="http://t.qq.com/p/t/19142128390414" from="3">昨天 15:46</a> <a href="http://t.qq.com" class="f" target="_blank">来自腾讯微博</a> </span><div class="funBox"><a href="#" class="relay" num="0">转播</a><span>|</span><a href="/p/t/19142128390414" class="comt" num="0">评论</a><span>|</span><div class="mFun"><a href="#">更多<em class="btn_ldrop"></em></a><div class="mFunDrop"><b></b><b class="mask"></b><p><a href="#" class="delBtn">删除</a></p><p><a href="#" class="fav">收藏</a></p><div class="shareBtn"><p><a href="#">分享</a></p></div><p><a href="http://t.qq.com/p/t/19142128390414" class="detail" target="_blank">详情</a></p></div></div></div></div> </div></li><li id="70539117104187" rel="1312960577" > <div class="userPic"><a href="http://t.qq.com/szw520yun" title="孙志炜(@szw520yun)"><img src="http://mat1.gtimg.com/www/mb/images/head_50.jpg"/></a></div><div class="msgBox"><div class="userName" rel="szw520yun"><strong><a href="http://t.qq.com/szw520yun" title="孙志炜(@szw520yun)">孙志炜</a>:</strong></div><div class="msgCnt">测试第二下2</div><div class="pubInfo"> <span class="left"> <a class="time" target="_blank" href="http://t.qq.com/p/t/70539117104187" from="3">昨天
我想在里面提取"提取网页文字咋这么男",请高手们帮帮忙,怎么提取出来,正则我老是写错。我主要是提取手机微博发表的信息,也就是说这个信息是变化的,每次我用手机发表后后面总会跟着一个“刚刚”,如果过上几分钟,“刚刚”就会变成“几分钟之前”发表了什么,就像这样:
title="孙志炜(@szw520yun)">孙志炜</a>:</strong></div><div class="msgCnt">提取好难过啊</div><div class="pubInfo"> <span class="left"> <a class="time" target="_blank" href="http://t.qq.com/p/t/50553073201528" from="3">刚刚</a> <a href="http://t.qq.com" class="f" target="_blank">来自腾讯微博</a> ,能不能每次提取时判断是不是“刚刚”就提取后面有“刚刚”的文字内容呢???
Regex reg = new Regex(@"(?<=<div[^>]*?class=""msgCnt"">)((?:(?!</?div).)*)</div>.*?<a[^>]*?>\s*刚刚.*?</a>");
Console.WriteLine(reg.Match(str).Groups[1].Value);
嘿嘿,大哥,我连续三个问题都是你给我解答的,这次也100%正确了,我用手机发表了:“如果成功”,真的提取出来了,提取效果:如果成功</div><div class="pubInfo"> <span class="left"> <a class="time" target="_blank" href="http://t.qq.com/p/t/13143067912704" from="2">刚刚</a> ,大哥,能把中间的那部分去除吗,尽量少点就行,不用全都去除?
Console.WriteLine(reg.Match(str).Groups[1].Value);
<div class="msgCnt">提取网页文字咋这么男</div>
提取的是<div>中间那部分的
你怎么取的?
感谢huangwenquan123热心的回答。
LS,一起学习吧
最后一个问题了。假设我要提取某个网页总有多少页数
网站的源码我提取出来了。
我要获得这段链接中的276这个数字,怎么办?href='/bxmp/20110418/276/O3S877I262ea0dFO.shtml'>
<!-- baidu_tc block_begin: {"type":"PAGE_TURNING", "action":"SHOW"} -->
<div class="page">
<div class="inner"><a class='on'>1</a><a href='/bxmp/20110418/1/O3S877I262ea0dFO.shtml'>2</a><a href='/bxmp/20110418/2/O3S877I262ea0dFO.shtml'>3</a><a href='/bxmp/20110418/3/O3S877I262ea0dFO.shtml'>4</a><a href='/bxmp/20110418/4/O3S877I262ea0dFO.shtml'>5</a><a href='/bxmp/20110418/5/O3S877I262ea0dFO.shtml'>6</a><a href='/bxmp/20110418/6/O3S877I262ea0dFO.shtml'>7</a><a href='/bxmp/20110418/7/O3S877I262ea0dFO.shtml'>8</a><a href='/bxmp/20110418/8/O3S877I262ea0dFO.shtml'>9</a><a href='/bxmp/20110418/9/O3S877I262ea0dFO.shtml'>10</a><a class='endgray' href='/bxmp/20110418/276/O3S877I262ea0dFO.shtml'>尾页</a><a class='end' href='/bxmp/20110418/1/O3S877I262ea0dFO.shtml'>下一页>></a></div>
</div>
<!-- baidu_tc block_end -->
现在我想要获得这个链接中276这个数字
href='/bxmp/20110418/[color=#FF00FF]276/O3S877I262ea0dFO.shtml'[/color]
<!-- baidu_tc block_begin: {"type":"PAGE_TURNING", "action":"SHOW"} -->
<div class="page">
<div class="inner"><a class='on'>1</a><a
href='/bxmp/20110418/1/O3S877I262ea0dFO.shtml'>2</a><a href='/bxmp/20110418/2/O3S877I262ea0dFO.shtml'>3</a><a href='/bxmp/20110418/3/O3S877I262ea0dFO.shtml'>4</a><a href='/bxmp/20110418/4/O3S877I262ea0dFO.shtml'>5</a><a href='/bxmp/20110418/5/O3S877I262ea0dFO.shtml'>6</a><a href='/bxmp/20110418/6/O3S877I262ea0dFO.shtml'>7</a><a href='/bxmp/20110418/7/O3S877I262ea0dFO.shtml'>8</a><a href='/bxmp/20110418/8/O3S877I262ea0dFO.shtml'>9</a><a href='/bxmp/20110418/9/O3S877I262ea0dFO.shtml'>10</a><a class='endgray' href='/bxmp/20110418/276/O3S877I262ea0dFO.shtml'>尾页</a><a class='end' href='/bxmp/20110418/1/O3S877I262ea0dFO.shtml'>下一页>></a></div>
</div>
<!-- baidu_tc block_end -->
System.IO.StreamReader getReader = new System.IO.StreamReader(this.webBrowser1.DocumentStream, System.Text.Encoding.GetEncoding("utf-8"));
textBox2.Text= getReader.ReadToEnd();
Regex reg = new Regex(@"(?<=<div[^>]*?class=""msgCnt"">)((?:(?!</?div).)*)</div>.*?<a[^>]*?>\s*刚刚.*?</a>");
foreach (Match m in reg.Matches(textBox2.Text))
textBox1.Text= m.Value;
string str = File.ReadAllText(@"F:\txt.txt", Encoding.GetEncoding("gb2312"));
Regex reg = new Regex(@"(?is)(?<=<div[^>]*?class=""inner""[^>]*?>(?:(?!</?div).)*)<a[^>]*?href=(['""\s]?)[^'""\s]+/(\d+)/[^\.]+?\.shtml\1[^>]*?>\s*尾页\s*</a>");
Console.WriteLine(reg.Match(str).Groups[2].Value);
Console.ReadLine();
//276
reg.Match(str).Groups[1].Value组1取,不要直接m.value