asp.net 抓取新闻
前台页面:
<!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 抓取新闻的更多相关文章
- ASP.NET抓取网页内容的实现方法
这篇文章主要介绍了ASP.NET抓取网页内容的实现方法,涉及使用HttpWebRequest及WebResponse抓取网页内容的技巧,需要的朋友可以参考下 一.ASP.NET 使用HttpWebRe ...
- ASP.NET抓取网页内容
原文:ASP.NET抓取网页内容 一.ASP.NET 使用HttpWebRequest抓取网页内容 这种方式抓取某些页面会失败 不过,有时候我们会发现,这个程序在抓取某些页面时,是获不到所需的内容的, ...
- 使用轻量级JAVA 爬虫Gecco工具抓取新闻DEMO
写在前面 最近看到Gecoo爬虫工具,感觉比较简单好用,所有写个DEMO测试一下,抓取网站 http://zj.zjol.com.cn/home.html,主要抓取新闻的标题和发布时间做为抓取测试对象 ...
- ASP.NET 抓取网页内容
(转)ASP.NET 抓取网页内容 ASP.NET 抓取网页内容-文字 ASP.NET 中抓取网页内容是非常方便的,而其中更是解决了 ASP 中困扰我们的编码问题. 需要三个类:WebRequest. ...
- asp.net抓取网页html源代码失败 只因UserAgent作怪
asp.net抓取网页html源代码,我想对于任何一个asp.net程序员来说都不再陌生,这是一个非常简单容易就能实现的功能.下面便是一个通用的asp.net获得网页源代码的程序. 首先引用 usin ...
- 使用jsoup抓取新闻信息
1,jsoup简介 jsoup 是一款Java 的HTML解析器,可直接解析某个URL地址.HTML文本内容.它提供了一套非常省力的API,可通过DOM,CSS以及类似于jQuery的操作方法来取出和 ...
- C# asp.net 抓取需要登录的网页内容 抓取asp.net登录验证的网站
private void btnASPNET_Click(object sender, EventArgs e) { Dictionary<string, s ...
- 三种asp.net 抓取网页源代码
/// <summary>方法一:比较推荐 /// 用HttpWebRequest取得网页源码 /// 对于带BOM的网页很有效,不管是什么编码都能正确识别 /// </summar ...
- 利用calibre抓取新闻
Adding your favorite news website calibre has a powerful, flexible and easy-to-use framework for dow ...
随机推荐
- 【最优比率生成树】poj2728 Desert King
最优比率生成树教程见http://blog.csdn.net/sdj222555/article/details/7490797 个人觉得很明白易懂,但他写的代码略囧. 模板题,但是必须Prim,不能 ...
- 20162312实验四Java Android简易开发
实验准备 Android Studio 的下载: Android Studio 安装教程 准备中遇到的问题 最大的问题就是电脑无法虚拟化,因为微星的型号太多,我只好在网上找了许多方案一个个试,最后终于 ...
- Java编程思想学习(二)----一切都是对象
2.1用应用操作对象 String s; 这里所创建的只是引用,并不是对象.如果此时向s发送一个消息,就会返回一个运行时错误.这是因为此时s没有和任何事物关联.因此,一种安全的做法是:创建一个引用的同 ...
- Manthan, Codefest 16 A. Ebony and Ivory 水题
A. Ebony and Ivory 题目连接: http://www.codeforces.com/contest/633/problem/A Description Dante is engage ...
- DN安卓2014版(5-9)
DN安卓2014版(5-9) 联系2g32@sina.com
- mormot日志
mormot日志 usesSynLog; if log = nil then // 日志 begin log := TSynLog.Add; log.Family.DestinationPath := ...
- 【redis】2.redis可视化工具安装使用
redis可视化工具:Redis Desktop Manager 1.redis桌面管理工具[可视化工具]下载 下载地址:https://redisdesktop.com/download 2.点击安 ...
- (救星啊)im-switch -s ibus错误:Error: no configuration file "ibus" exists.
转自:http://www.cnblogs.com/csulennon/p/4194902.html 在虚拟机上安装Ubuntu14.04 后安装ibus输入法,万万没想到在切换输入法的时候居然出错了 ...
- brew 安装PHP的配置文件所在位置
- Linux /sbin/service脚本一个基本无影响的bug
CentOS提供了一个启动服务的功能:service [service name] (start|stop|restart|...),此功能的执行脚本为/sbin/service. 今天看了下此脚本, ...