前台页面:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.2.js"></script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Panel ID="Panel1" runat="server">
网址:<asp:TextBox ID="txtUrl" runat="server"></asp:TextBox>
<asp:Button ID="btnGet" runat="server" Text="RSS" OnClick="btnGet_Click" />
</asp:Panel>
</div>
</form>
</body>
</html>

后台代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Net;
using System.Text.RegularExpressions; public partial class Default7 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ }
protected void btnGet_Click(object sender, EventArgs e)
{
string strurl = txtUrl.Text.ToString(); //欲获取的网页地址 要 http://
WebClient myWebClient = new WebClient(); //创建WebClient实例myWebClient
//获取或设置用于对向 Internet 资源的请求进行身份验证的网络凭据。
myWebClient.Credentials = CredentialCache.DefaultCredentials;
//从资源下载数据并返回字节数组。(加@是因为网址中间有"/"符号)
byte[] pagedata = myWebClient.DownloadData(@strurl);
//string result = Encoding.Default.GetString(pagedata); //如果获取网站页面采用的是GB2312,则使用这句
string result = Encoding.GetEncoding("GB2312").GetString(pagedata); //如果获取网站页面采用的是UTF-8,则使用这句
Regex regex = new Regex("<div class=\"mark\">([\\s\\S]*)</div>([\\s\\S]*)<div id=\"ajax_page\">", RegexOptions.Compiled);
Match match= regex.Match(result);
if (match.Success)
{
result = match.Groups[0].Value;
}
Response.Write(result); //在WEB页中显示获取的内容
Panel1.Visible = false; } /// <summary>
/// 返回分析数据
/// </summary>
/// <param name="regexString">正则</param>
/// <param name="html">HTML</param>
/// <param name="group">分组长度</param>
/// <returns>数据</returns>
//public List<string> GetData(string regexString, string html, int group)
//{
// List<string> result = new List<string>();
// Regex regex = new Regex(regexString, RegexOptions.IgnoreCase);
// MatchCollection mc = regex.Matches(html);
// for (int count = 0; count < mc.Count; count++)
// {
// Match m = mc[count];
// for (int index = 0; m.Groups[index].Value != ""; index++)
// {
// string value = m.Groups[index].Value;
// if (count % group != 2)
// value = Regex.Replace(value, "&", "");
// if (value == "")
// {
// result.RemoveRange((result.Count / group) * group, result.Count % group);
// count = (count / group) * group + group - 1;
// break;
// }
// result.Add(value);
// }
// }
// return result;
//} /// <summary>
/// 返回分析数据
/// </summary>
/// <param name="regexString">正则</param>
/// <param name="html">HTML</param>
/// <returns>数据</returns>
public List<string> GetData(string regexString, string html)
{
List<string> result = new List<string>();
Regex regex = new Regex(regexString, RegexOptions.IgnoreCase);
MatchCollection mc = regex.Matches(html);
for (int count = 0; count < mc.Count; count++)
{
Match m = mc[count];
for (int index = 0; m.Groups[index].Value != ""; index++)
{
result.Add(m.Groups[index].Value);
}
}
return result;
} }

  

asp.net 抓取新闻的更多相关文章

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

    这篇文章主要介绍了ASP.NET抓取网页内容的实现方法,涉及使用HttpWebRequest及WebResponse抓取网页内容的技巧,需要的朋友可以参考下 一.ASP.NET 使用HttpWebRe ...

  2. ASP.NET抓取网页内容

    原文:ASP.NET抓取网页内容 一.ASP.NET 使用HttpWebRequest抓取网页内容 这种方式抓取某些页面会失败 不过,有时候我们会发现,这个程序在抓取某些页面时,是获不到所需的内容的, ...

  3. 使用轻量级JAVA 爬虫Gecco工具抓取新闻DEMO

    写在前面 最近看到Gecoo爬虫工具,感觉比较简单好用,所有写个DEMO测试一下,抓取网站 http://zj.zjol.com.cn/home.html,主要抓取新闻的标题和发布时间做为抓取测试对象 ...

  4. ASP.NET 抓取网页内容

    (转)ASP.NET 抓取网页内容 ASP.NET 抓取网页内容-文字 ASP.NET 中抓取网页内容是非常方便的,而其中更是解决了 ASP 中困扰我们的编码问题. 需要三个类:WebRequest. ...

  5. asp.net抓取网页html源代码失败 只因UserAgent作怪

    asp.net抓取网页html源代码,我想对于任何一个asp.net程序员来说都不再陌生,这是一个非常简单容易就能实现的功能.下面便是一个通用的asp.net获得网页源代码的程序. 首先引用 usin ...

  6. 使用jsoup抓取新闻信息

    1,jsoup简介 jsoup 是一款Java 的HTML解析器,可直接解析某个URL地址.HTML文本内容.它提供了一套非常省力的API,可通过DOM,CSS以及类似于jQuery的操作方法来取出和 ...

  7. C# asp.net 抓取需要登录的网页内容 抓取asp.net登录验证的网站

    private void btnASPNET_Click(object sender, EventArgs e)        {            Dictionary<string, s ...

  8. 三种asp.net 抓取网页源代码

    /// <summary>方法一:比较推荐 /// 用HttpWebRequest取得网页源码 /// 对于带BOM的网页很有效,不管是什么编码都能正确识别 /// </summar ...

  9. 利用calibre抓取新闻

    Adding your favorite news website calibre has a powerful, flexible and easy-to-use framework for dow ...

随机推荐

  1. 【数论】【莫比乌斯反演】【线性筛】bzoj2301 [HAOI2011]Problem b

    对于给出的n个询问,每次求有多少个数对(x,y),满足a≤x≤b,c≤y≤d,且gcd(x,y) = k,gcd(x,y)函数为x和y的最大公约数. 100%的数据满足:1≤n≤50000,1≤a≤b ...

  2. 【计算几何】【bitset】Gym - 101412G - Let There Be Light

    三维空间中有一些(<=2000)气球,一些光源(<=15),给定一个目标点,问你在移除不超过K个气球的前提下,目标点所能接受到的最大光照. 枚举每个光源,预处理其若要照射到光源,需要移走哪 ...

  3. 【树形dp】Codeforces Round #405 (rated, Div. 1, based on VK Cup 2017 Round 1) B. Bear and Tree Jumps

    我们要统计的答案是sigma([L/K]),L为路径的长度,中括号表示上取整. [L/K]化简一下就是(L+f(L,K))/K,f(L,K)表示长度为L的路径要想达到K的整数倍,还要加上多少. 于是, ...

  4. HDU 5638 Toposort 拓扑排序 优先队列

    Toposort 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5638 Description There is a directed acycli ...

  5. CentOS 6.9下配置安装KVM

    注意:KVM一切安装和运行都是在root用户下完成的,并且只有root才能支持某些软件. 一.准备工作: 1.查看系统版本.内核版本 ##查看系统版本 # cat /etc/redhat-releas ...

  6. Android客户端面试题集锦

    声明:本文问题来自但不限于Xoper.ducky大牛的面试总结,网址:http://www.nowcoder.com/discuss/3043,欢迎各位进行补充 JAVA SE 1. 九种基本数据类型 ...

  7. 【mybatis】分别按照 天 月 年 统计查询

    页面统计想通过 天 月 年 分别来展示统计效果, 那么查询SQL拼接如下: select *, <if test="groupType == 1"> DATE_FORM ...

  8. jquery缩写,显示隐藏

    $(".a").css("display")=="none" ?$(".a").css("display&qu ...

  9. 网络上的等待事件 —— SQL*Net message from client/dblink

    SQL*Net message from client SQL> select event#,name,parameter1,parameter2,parameter3 from v$event ...

  10. 【FireMonkey】StyleBook使用方法

    近期在开发一个团队文档管理工具,使用Embarcadero的XE2-C++builder进行界面开发,使用Firemonkey框架. 而这个框架十分有趣!可能吸引界面开发者的就是这个StyleBook ...