























获取网页的内容,以js方式输出,没有测试性能怎么样。
1.调用页面
<html>
...
<body>
...
<script type="text/javascript" src="GetNews.aspx"></script>
...
</body>
<html>
2.GetNews.aspx,只保留第一行,其它代码不要
<%@ Page language="c#" Codebehind="GetNews.aspx.cs" AutoEventWireup="false" Inherits="PhantomWeb.Components.GetNews" %>
3.GetNews.aspx.cs
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Net;
using System.Text.RegularExpressions;
namespace PhantomWeb.Components
{
/********************************************
Author: WebWei
Homepage: http://webwei.cnblogs.com/
Email: webwei.net@gmail.com 
********************************************/
/// <summary>
/// GetNews 的摘要说明。
/// 获取新闻网最新信息
/// 输出JS代码
/// </summary>
public class GetNews : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
GetWebCode();
}
public void GetWebCode()
{
string url=@"http://news.****.edu.cn/"; //获取输入的网页地址
string result="";
try
{
WebClient wb=new WebClient(); //创建一个WebClient实例
//获取或设置用于对向 Internet 资源的请求进行身份验证的网络凭据。(可有可无)
//wb.Credentials=CredentialCache.DefaultCredentials;
//从资源下载数据并返回字节数组。(加@是因为网址中间有"/"符号)
byte[] pagedata=wb.DownloadData(@url);
//转换字符、
result=RebulidNews(Encoding.Default.GetString(pagedata));
}
catch(Exception ex)
{
result=ex.Message;
}
Response.Write(result);//输出JS代码
}
public string RebulidNews(string pagedata)
{
string result=@"document.writeln(""<table>"");";
Regex re = new Regex(@"<a href=""http://news.****.edu.cn/(?<id>.*?)""[^>]*target=""_blank"" class=""a2"">(?<text>.*?)</a>(?<date>.*?)</td>", RegexOptions.IgnoreCase);
int i=0;
for(Match m=re.Match(pagedata);m.Success&&i<10;m=m.NextMatch())
{
result+=@"document.writeln(""<tr><td>·<a href=\""http://news.****.edu.cn/";
result+=@m.Groups["id"].Value.Trim();
result+=@"\"" target=\""_blank\"">";
result+=@m.Groups["text"].Value.Trim().Replace(" color='#6495ED'>",">").Replace(" size=2>",">");
result+=@"</a><font color='#727272'>";
result+=@m.Groups["date"].Value.Trim();
result+=@"</font></td></tr>"");";
i++;
}
result+=@"document.writeln(""</table>"");";
return result;
}
Web 窗体设计器生成的代码
}
}
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。