惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

The GitHub Blog
The GitHub Blog
Y
Y Combinator Blog
爱范儿
爱范儿
P
Proofpoint News Feed
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Microsoft Security Blog
Microsoft Security Blog
小众软件
小众软件
F
Full Disclosure
酷 壳 – CoolShell
酷 壳 – CoolShell
Recent Announcements
Recent Announcements
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
F
Fortinet All Blogs
Google DeepMind News
Google DeepMind News
Jina AI
Jina AI
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
I
Intezer
S
SegmentFault 最新的问题
S
Schneier on Security
V
Vulnerabilities – Threatpost
T
Tenable Blog
P
Privacy & Cybersecurity Law Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Latest news
Latest news
Simon Willison's Weblog
Simon Willison's Weblog
D
DataBreaches.Net
L
LINUX DO - 热门话题
宝玉的分享
宝玉的分享
Hugging Face - Blog
Hugging Face - Blog
Stack Overflow Blog
Stack Overflow Blog
SecWiki News
SecWiki News
H
Hacker News: Front Page
aimingoo的专栏
aimingoo的专栏
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
T
Threatpost
罗磊的独立博客
L
LangChain Blog
The Last Watchdog
The Last Watchdog
Recent Commits to openclaw:main
Recent Commits to openclaw:main
K
Kaspersky official blog
腾讯CDC
阮一峰的网络日志
阮一峰的网络日志
N
News | PayPal Newsroom
美团技术团队
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
D
Docker
T
The Blog of Author Tim Ferriss
N
Netflix TechBlog - Medium
博客园 - 【当耐特】
Cyberwarzone
Cyberwarzone

博客园 - 鬼手

有趣的网页效果 杀毒软件测试代码 Gmail邀请函 Office的组织结构图 博客园出问题了? 利用js在网页中插入flash JS实现marquee效果 关于IE WebControl 无法加载XML的问题(后续) Ajax.net Onloading Example KBSBBS安装 SQL企业管理器打不开的解决办法 IE WebControl 无法显示XML的问题 自己制作的一些网站 开始要忙了 记录一些网址 备忘 获取地址栏参数 用JAVASCRIPT 控制FRMAE的子窗口 在css中加入事件
获取网页内容
鬼手 · 2006-11-11 · via 博客园 - 鬼手

获取网页的内容,以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 窗体设计器生成的代码
    }

}