前台页面:

<!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. 【优先队列+贪心】POJ2431-Expedition

    解题方法详见<挑战程序设计竞赛(第二版)>P74-75.注意首先要对加油站以位置为关键字快排,不要遗忘把终点视作一个加油量为0的加油站,否则最终只能到达终点前的加油站. /*优先队列*/ ...

  2. python安装BeautifulSoup

    1.先下载pip https://pypi.python.org/pypi/pip 安装pip cd到路径 python setuo.py install 2.添加目录到环境变量中 xxx\Pytho ...

  3. Java如何判断线程池所有任务是否执行完毕

    import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class Tes ...

  4. 用GDB 调试Java程序

      陈皓 http://blog.csdn.net/haoel 背景 想要使用GDB调试程序,就需要用GNU的编译器编译程序.如:用GCC编译的C/C++的程序,才能用GDB调试.对于Java程序也是 ...

  5. Winfrom 使用TabControl控件模拟程序向导步骤

    .NET : 隐藏TabControl的标签栏   在给应用程序添加一个向导的做法有很多,但其中比较简便易行的是使用TabControl.如下图所示 但有一个小小的美中不足,就是:作为向导而言,我们可 ...

  6. 原生js封装的一些jquery方法

    用js封装一些常用的jquery方法 记录一下 hasClass:判断是否有class function hasClass(ele, cls) { if (!ele || !cls) return f ...

  7. 在u-boot中添加命令

    转:http://www.embedu.org/Column/Column464.htm 作者:曾宏安,华清远见嵌入式学院讲师. u-boot是嵌入式系统中广泛使用的一种bootloader.它不仅支 ...

  8. 封装log4cp p

    log4cpp 是参考 log4j 所写的 c++ 版本的写 log 的库.可以在这里下载   http://log4cpp.sourceforge.net/   我的使用方法是: 1,定义了一个 _ ...

  9. 19个三维GIS软件对比

    19个三维GIS软件对比 麦豆科研技术中心 days ago 我国GIS经过三十多年的发展,理论和技术日趋成熟,在传统二维GIS已不能满足应用需求的情况下,三维GIS应运而生,并成为GIS的重要发展方 ...

  10. Flask如何使用https?

    1 安装python 的 openssl 的类库 pip install pyOpenSSL 2 在 Flask 的代码中可以直接使用,注意ssl_context的值必须是adhoc from fla ...