码迷,mamicode.com
首页 > Web开发 > 详细

asp.net如何抓取其他网站的内容

时间:2014-05-17 22:34:49      阅读:423      评论:0      收藏:0      [点我收藏+]

标签:class   code   c   ext   http   a   

1. 需要引用的类库 


using System.Net; 
using System.IO; 
using System.Text; 
using System.Text.RegularExpressions; 


2. 获取其他网站网页内容的关键代码 


WebRequest request = WebRequest.Create("http://目标网址.com/"); 
WebResponse response = request.GetResponse(); 
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("gb2312")); 
//reader.ReadToEnd() 表示取得网页的源码 
TextBox1.Text = reader.ReadToEnd(); 


3. 获取其他网站网页源码之后通过{正则表达式}帅选有用信息 


MatchCollection TitleMatchs = Regex.Matches(reader.ReadToEnd(), @"发表评论</a></p></div><div class=""body"">([\s\S]*?)</div><div class=""share"">", RegexOptions.IgnoreCase | RegexOptions.Multiline); 
foreach (Match NextMatch in TitleMatchs) 

s += "<br>" + NextMatch.Groups.Value; 
TextBox1.Text += "\n" + NextMatch.Groups.Value; 


RegexOptions.IgnoreCase: 表示不区分大小写, 一般网站源码大小写不敏感所以取消之. 

RegexOptions.Multiline: 表示对多行内容进行帅选. 
4. 大功告成 
不上图了! 影响不好! 见谅见谅 

asp.net如何抓取其他网站的内容,布布扣,bubuko.com

asp.net如何抓取其他网站的内容

标签:class   code   c   ext   http   a   

原文地址:http://www.cnblogs.com/q101301/p/3734537.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!