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 ...
随机推荐
- 【FFT卷积】BZOJ3527-力
[题目大意] [思路] 很好这很FFT…… 想了半天也没明白到底什么是卷积∑的上下界,我当初学的时候没说一定要从0开始啊quq 我还是背不出FFT的模板我要狗带了 我上面写的什么乱七八糟的,要什么数学 ...
- 记录Debug神经网络的方法
debugNNIntroduction to debugging neural networksThe following advice is targeted at beginners to neu ...
- [转] Log4j 配置 的webAppRootKey参数问题
在tomcat下部署两个或多个项目时,web.xml文件中最好定义webAppRootKey参数,如果不定义,将会缺省为“webapp.root”,如下:<!-- 应用路径 --> < ...
- mysql交叉表查询解决方案整理
交叉表是一种常用的分类汇总查询.使用交叉表查询,可以显示表中某个字段的汇总值,并将它们分组,其中一组列在数据表的左侧,另一组列在数据表的上部.行和列的交叉处可以对数据进行多种汇总计算,如:求和.平均值 ...
- Web安全开发指南--异常错误处理与日志审计
1.异常错误处理与日志审计 5.1.日志审计系统安全规则 1 日志系统能够记录特定事件的执行结果(比如 成功或失败). 确保日志系统包含如下重要日志信息: 1. 日志发生的时间: 2. 事件的严重 ...
- andriod inputType
<EditText Android:layout_width="fill_parent" android:layout_height="wrap_content&q ...
- xfs mount and repair
sudo mount -t xfs /dev/sdb1 /storage xfs文件系统修复方法 2017年12月03日 10:14:19 阅读数:2749 1. 前言 首先尝试mount和umoun ...
- Git系列四之在本地服务器搭建gitlab仓库管理
1.Git仓库管理 现在本地已经创建了git仓库,又在gitlab上创建了一个git仓库,并且让这两个仓库进行远程同步,这样gitlab仓库既可以备份也可以与他人协作管理远程仓库以及根据需要推送或拉取 ...
- centos使用密钥替换密码登录服务器
一.首先登陆centos,切换用户,切换到你要免密码登陆的用户,进入到家目录,以下我以admin为例,命令:su admincd ~ 二.创建钥匙,命令:ssh-keygen -t rsa,一路按Y搞 ...
- sqlite 二进制字段 (zz)
有时我们用数据库存储文件,需要用到二进制字段,下面列常用方法. 1.写二进制数据 sqlite3 * db; int result; char **errmsg =NULL; result = sql ...