[ 转]C#(ASP.net)从其他网站抓取内容并截取有用信息

本文转自:http://www.cnblogs.com/henw/archive/2011/09/23/2186387.html

1. 需要引用的类库

?
1
2
3
4
using System.Net;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;

 

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

?
1
2
3
4
5
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. 获取其他网站网页源码之后通过{正则表达式}帅选有用信息

 

?
1
2
3
4
5
6
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[1].Value;
     TextBox1.Text += "\n" + NextMatch.Groups[1].Value;
}

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

 

RegexOptions.Multiline: 表示对多行内容进行帅选.

4. 大功告成

不上图了! 影响不好! 见谅见谅

 

欢迎大家一起讨论!

示例源码下载

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值