手机
当前位置:查字典教程网 >编程开发 >asp.net教程 >ASP.net(C#)从其他网站抓取内容并截取有用信息的实现代码
ASP.net(C#)从其他网站抓取内容并截取有用信息的实现代码
摘要:1.需要引用的类库复制代码代码如下:usingSystem.Net;usingSystem.IO;usingSystem.Text;usin...

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><divbody"">([sS]*?)</div><divshare"">", 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. 大功告成

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

文中代码打包下载

【ASP.net(C#)从其他网站抓取内容并截取有用信息的实现代码】相关文章:

ASP.NET抓取网页内容的实现方法

asp.net传多个值到其它页面的具体实现

Asp.Net Cache缓存使用代码

ASP.net 生成缩略图的实例源代码

asp.net 无重复随机数代码

asp.net 操作excel的实现代码

ASP.NET Ajax级联DropDownList实现代码

ASP.Net 图片存入数据库的实现代码

Asp.net下载功能的解决方案代码

asp.net 生成曲线图实现代码

精品推荐
分类导航