先说明问题:
下面html代码很多,所以我的难题就是要在一大堆代码里找到我需要的信息。请查找字符串“http://user.qzone.qq.com/”就看得到很多重复的信息,我需要的就是http://user.qzone.qq.com/760860458后面的QQ号码,注意,代码里的每个QQ会重复2次,所以需要过滤重复信息。
实际情况会有很多代码,所以手工来做太蛋疼了。请高人出手,解决我的难题吧。我希望使用C#来操作,收集一组QQ号码我会保存到SQLServer里,这个工作我自己会。
1 <div id="p_0"> 2 <div class="bbor3 item" id="visitor-item-718934403"> 3 <div class="userinfo"><a class="ui_avatar" id="visitor-icon-718934403" target="_blank" href="http://user.qzone.qq.com/718934403"><img link="nameCard_718934403" class="q_namecard pic" src="http://qlogo4.store.qq.com/qzone/718934403/718934403/50"></a> 4 <p id="visitor-name-718934403" class="name"><a link="nameCard_718934403" class="c_tx q_namecard" target="_blank" href="http://user.qzone.qq.com/718934403">CoCo</a> <a href="http://vip.qzone.com/?login=qq" class="none" target="_blank"></a> <img class="ico_online" title="QQ在线" src="http://imgcache.qq.com/ac/b.gif"></p> 5 <div id="visitor-content-718934403" class="contxt"> 6 <p id="friend-mood-718934403">说说:<span id="mood-718934403"></span></p> 7 <p id="friend-update-718934403"></p> 8 <p class="blog" id="friend-content-718934403"></p> 9 </div> 10 </div> 11 <div id="visitor-tools-718934403" class="tools"><a class="c_tx unline" target="_blank" href="http://user.qzone.qq.com/718934403/profile">个人档</a><br> 12 <a class="unline" onclick="addFriend(718934403);return false;" href="javascript:;">加好友</a><br> 13 </div> 14 </div> 15 <div class="bbor3 item" id="visitor-item-760860458"> 16 <div class="userinfo"><a class="ui_avatar" id="visitor-icon-760860458" target="_blank" href="http://user.qzone.qq.com/760860458"><img link="nameCard_760860458" class="q_namecard pic" src="http://qlogo3.store.qq.com/qzone/760860458/760860458/50"></a> 17 <p id="visitor-name-760860458" class="name"><a link="nameCard_760860458" class="c_tx q_namecard" target="_blank" href="http://user.qzone.qq.com/760860458">灯火阑珊</a> <a href="http://vip.qzone.com/?login=qq" class="none" target="_blank"></a> <img class="ico_online" title="QQ在线" src="http://imgcache.qq.com/ac/b.gif"></p> 18 <div id="visitor-content-760860458" class="contxt"> 19 <p id="friend-mood-760860458">说说:<span id="mood-760860458"></span></p> 20 <p id="friend-update-760860458"></p> 21 <p class="blog" id="friend-content-760860458"></p> 22 </div> 23 </div> 24 <div id="visitor-tools-760860458" class="tools"><a class="c_tx unline" target="_blank" href="http://user.qzone.qq.com/760860458/profile">个人档</a><br> 25 <a class="unline" onclick="addFriend(760860458);return false;" href="javascript:;">加好友</a><br> 26 </div> 27 </div> 28 <div class="bbor3 item" id="visitor-item-1299052714"> 29 <div class="userinfo"><a class="ui_avatar" id="visitor-icon-1299052714" target="_blank" href="http://user.qzone.qq.com/1299052714"><img link="nameCard_1299052714" class="q_namecard pic" src="http://qlogo3.store.qq.com/qzone/1299052714/1299052714/50"></a> 30 <p id="visitor-name-1299052714" class="name"><a link="nameCard_1299052714" class="c_tx q_namecard" target="_blank" href="http://user.qzone.qq.com/1299052714">涣散了、眼眸旳色泽°</a> <a href="http://vip.qzone.com/?login=qq" class="none" target="_blank"></a> <img class="ico_online" title="QQ在线" src="http://imgcache.qq.com/ac/b.gif"></p> 31 <div id="visitor-content-1299052714" class="contxt"> 32 <p id="friend-mood-1299052714">说说:<span id="mood-1299052714"></span></p> 33 <p id="friend-update-1299052714"></p> 34 <p class="blog" id="friend-content-1299052714"></p> 35 </div> 36 </div> 37 <div id="visitor-tools-1299052714" class="tools"><a class="c_tx unline" target="_blank" href="http://user.qzone.qq.com/1299052714/profile">个人档</a><br> 38 <a class="unline" onclick="addFriend(1299052714);return false;" href="javascript:;">加好友</a><br> 39 </div> 40 </div> 41 <div class="bbor3 item" id="visitor-item-869055812"> 42 <div class="userinfo"><a class="ui_avatar" id="visitor-icon-869055812" target="_blank" href="http://user.qzone.qq.com/869055812"><img link="nameCard_869055812" class="q_namecard pic" src="http://qlogo1.store.qq.com/qzone/869055812/869055812/50"></a> 43 <p id="visitor-name-869055812" class="name"><a link="nameCard_869055812" class="c_tx q_namecard" target="_blank" href="http://user.qzone.qq.com/869055812">芮恩</a> <a href="http://vip.qzone.com/?login=qq" class="none" target="_blank"></a> <img class="ico_online" title="QQ在线" src="http://imgcache.qq.com/ac/b.gif"></p> 44 <div id="visitor-content-869055812" class="contxt"> 45 <p id="friend-mood-869055812">说说:<span id="mood-869055812"></span></p> 46 <p id="friend-update-869055812"></p> 47 <p class="blog" id="friend-content-869055812"></p> 48 </div> 49 </div> 50 <div id="visitor-tools-869055812" class="tools"><a class="c_tx unline" target="_blank" href="http://user.qzone.qq.com/869055812/profile">个人档</a><br> 51 <a class="unline" onclick="addFriend(869055812);return false;" href="javascript:;">加好友</a><br> 52 </div> 53 </div> 54 </div>
如果只是为了收集QQ号建议用正则表达式做:
http://user.qzone.qq.com/(\d+)
那代码怎么写?只提示一点不能够解决问题哦
@Om.Jj.Wang:
var html = @"1 <div id='p_0'> 2 <div class='bbor3 item' id='visitor-item-718934403'> 3 <div class='userinfo'><a class='ui_avatar' id='visitor-icon-718934403' target='_blank' href='http://user.qzone.qq.com/718934403'><img link='nameCard_718934403' class='q_namecard pic' src='http://qlogo4.store.qq.com/qzone/718934403/718934403/50'></a> 4 <p id='visitor-name-718934403' class='name'><a link='nameCard_718934403' class='c_tx q_namecard' target='_blank' href='http://user.qzone.qq.com/718934403'>CoCo</a> <a href='http://vip.qzone.com/?login=qq' class='none' target='_blank'></a> <img class='ico_online' title='QQ在线' src='http://imgcache.qq.com/ac/b.gif'></p> 5 <div id='visitor-content-718934403' class='contxt'> 6 <p id='friend-mood-718934403'>说说:<span id='mood-718934403'></span></p> 7 <p id='friend-update-718934403'></p> 8 <p class='blog' id='friend-content-718934403'></p> 9 </div>"; var reg = new Regex(@"http://user.qzone.qq.com/(?<num>\d+)"); var matchs=reg.Matches(html); var nums = new List<int>(); for (var i = 0; i < matchs.Count; i++) { var num = Int32.Parse(matchs[i].Groups["num"].Value); if (!nums.Contains(num)) { nums.Add(num); } }
最后得到的nums里就是你要的QQ号了
@artwl: 弱弱的问一句,在哪里能把你的答案设置为最佳答案。非常感谢你,代码非常好,完全解决了问题,谢谢。
@Om.Jj.Wang: 在问题下面有个结帖按钮啊
$('#p_0').match(/http://user.qzone.qq.com/(\d+)/g).text();
试试这个。