如何获取当前页面的HTML? [英] How get html of current page?

查看:540
本文介绍了如何获取当前页面的HTML?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要分析当前页面的HTML。
我怎样才能得到当前页的HTML代码中,在asp.net?

I want parse the html of current page. How can I get the html of current page for that in asp.net?

先谢谢了。

推荐答案

作为客户端

在Internet Explorer

In Internet explorer

右键单击浏览器 - >查看源代码

Right click on the browser --> View source

在Firefox

右键单击浏览器 - >查看页面源代码

Right click on the browser --> View Page Source

服务器端

您可以覆盖网页的渲染方法来捕获在服务器端的HTML源代码。

You can override the page's render method to capture the HTML source on the server-side.

protected override void Render(HtmlTextWriter writer)
{
    // setup a TextWriter to capture the markup
    TextWriter tw = new StringWriter();
    HtmlTextWriter htw = new HtmlTextWriter(tw);

    // render the markup into our surrogate TextWriter
    base.Render(htw);

    // get the captured markup as a string
    string pageSource = tw.ToString();

    // render the markup into the output stream verbatim
    writer.Write(pageSource);

    // remove the viewstate field from the captured markup
    string viewStateRemoved = Regex.Replace(pageSource,
        "<input type=\"hidden\" name=\"__VIEWSTATE\" id=\"__VIEWSTATE\" value=\".*?\" />",
        "", RegexOptions.IgnoreCase);

    // the page source, without the viewstate field, is in viewStateRemoved
    // do what you like with it
}

这篇关于如何获取当前页面的HTML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆