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 ...
随机推荐
- 【动态规划】CDOJ1651 Uestc的命运之旅
要处理从四个角出发的答案.最后枚举那个交点,然后讨论一下来的方向即可. #include<cstdio> #include<algorithm> using namespace ...
- 【SPFA】POJ3259-Wormhole
普通的SPFA的负环判定.犯了三个错误,全部写在注释里了. #include<iostream> #include<cstdio> #include<cstring> ...
- 重拾vue2
Vue组件 一.组件介绍 每一个组件都是一个vue实例 每个组件均具有自身的模板template,根组件的模板就是挂载点 每个组件模板只能拥有一个根标签 子组件的数据具有作用域,以达到组件的复用 二. ...
- Train Problem I(模拟栈)
题意:模拟栈,试问使用2个栈,能否使得串1变为串2 思路:模拟,经典问题,注意只要相同的元素放到栈顶后就不会再移动了,只需要考虑剩下的元素,因此每次只考虑一个元素的进入方式. #include< ...
- bootstrap学习(全局CSS样式)(一)
布局容器 bootstrap需要为页面内容和栅格系统包裹一个.container容器.我们提供了两个作词用处的类.注意,由于padding等属性的原因,这两种容器类不能互相嵌套. .container ...
- TSQL语言基础笔记之单表查询
db_id()函数 返回对象的id,如果返回的对象为null,则判断不存在 if db_id('testdb') is null --判断数据库是否存在 create database testdb ...
- 莫名其妙的float:left; 不能使元素紧贴父级的坑
这是项目中遇到的一个CSS的坑,做个记录,主要的原因还是浮动后脱离文档流,两个浮动的元素处于同一文档流中会相互影响位置的问题: 先上代码吧: 效果预览地址:浮动不能靠左的情况; 原本红色模块应该处于蓝 ...
- Android-25种开源炫酷动画框架
前言 忙碌的工作终于可以停息一段时间了,最近突然有一个想法,就是自己写一个app,所以找了一些合适开源控件,这样更加省时,再此分享给大家,希望能对大家有帮助,此博文介绍的都是UI上面的框架,接下来会有 ...
- Mount CIFS
mount -t cifs -o username="共享用户",password="密码" //ip/sharing_folder /mountpoint [ ...
- mysql5.7用户密码策略问题
密码策略问题 ERROR 1819 (HY000): Your password does not satisfy the current policy requirements 查看 mysql 初 ...