使用 c# 或 vb.net 获取最终生成的 html 源代码 [英] Get the final generated html source using c# or vb.net

查看:23
本文介绍了使用 c# 或 vb.net 获取最终生成的 html 源代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 VB.net 或 c#,如何获取生成的 HTML 源代码?

要获取页面的 html 源,我可以在下面使用它,但这不会获取生成的源,它不会包含任何由浏览器中的 javascript 动态添加的 html.如何获取最终生成的 HTML 源代码?

谢谢

WebRequest req = WebRequest.Create("http://www.asp.net");WebResponse res = req.GetResponse();StreamReader sr = new StreamReader(res.GetResponseStream());字符串 html = sr.ReadToEnd();

如果我在下面尝试这个,那么它会返回没有注入 JavaScript 代码的文档

公共类Form1Dim WB as WebBrowser = 没有Private Sub Form1_Load(sender As Object, e As EventArgs) 处理 MyBase.LoadWB = 新的 WebBrowser()Me.Controls.Add(WB)AddHandler WB.DocumentCompleted, AddressOf WebBrowser1_DocumentCompletedWB.Navigate("mysite/Default.aspx")结束子Private Sub WebBrowser1_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs)'Dim htmlcode As String = WebBrowser1.Document.Body.OuterHtml()Dim s As String = WB.DocumentText结束子结束班

HTML 返回

<html xmlns="http://www.w3.org/1999/xhtml"><head runat="服务器"><title></title><身体><form id="form1" runat="server"><div id="center_text_panel">//测试文本这个文本应该在这里

</表单><script type="text/javascript">document.getElementById("center_text_panel").innerText = "测试文本";

解决方案

您可以使用 WebKit.NET

看这里官方教程

这样不仅可以抓取源码,还可以通过pageload事件处理javascript.

webKitBrowser1.Navigate(MyURL)

然后,处理 DocumentCompleted 事件,并且:

private documentContent = webKitBrowser1.DocumentText

编辑 - 这可能是更好的开源 WebKit 选项:http://code.google.com/p/open-webkit-sharp/

using VB.net or c#, How do I get the generated HTML source?

To get the html source of a page I can use this below but this wont get the generated source, it won't contain any of the html that was added dynamically by the javascript in the browser. How do I get the the final generated HTML source?

thanks

WebRequest req = WebRequest.Create("http://www.asp.net"); 
WebResponse res = req.GetResponse(); 
StreamReader sr = new StreamReader(res.GetResponseStream()); 
string html = sr.ReadToEnd();

if I try this below then it returns the document with out the JavaScript code injected

Public Class Form1

    Dim WB As WebBrowser = Nothing

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        WB = New WebBrowser()
        Me.Controls.Add(WB)
        AddHandler WB.DocumentCompleted, AddressOf WebBrowser1_DocumentCompleted


        WB.Navigate("mysite/Default.aspx")

    End Sub

    Private Sub WebBrowser1_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs)


        'Dim htmlcode As String = WebBrowser1.Document.Body.OuterHtml()
        Dim s As String = WB.DocumentText

    End Sub
End Class

HTML returned

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>

</head>
<body>
    <form id="form1" runat="server">
    <div id="center_text_panel">
    //test text  this text should be here
    </div>
    </form>
</body>
</html>

    <script type="text/javascript">

        document.getElementById("center_text_panel").innerText = "test text";


    </script>

解决方案

You can use WebKit.NET

Look here for official tutorials

This can not only grab the source, but also process javascript through the pageload event.

webKitBrowser1.Navigate(MyURL)

Then, handle the DocumentCompleted event, and:

private documentContent = webKitBrowser1.DocumentText

Edit - This might be the better open source WebKit option: http://code.google.com/p/open-webkit-sharp/

这篇关于使用 c# 或 vb.net 获取最终生成的 html 源代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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