Get the final generated html source using c# or vb.net

前端 未结 3 1590
野的像风
野的像风 2020-11-27 22:25

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 c

3条回答
  •  我在风中等你
    2020-11-27 23:02

    Just put a webbrowser control to your form and you flowing code:

     webBrowser1.Navigate("YourLink");
    
         private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
            {
               string htmlcode= webBrowser1.Document.Body.InnerHtml;//Or Each Filed Or element..//WebBrowser.DocumentText
            }
    

    Edited

    for getting also html code that generated dynamically by java script code you have two way:

    1. run flowing code after webBrowser1_DocumentCompleted Event
     StringBuilder htmlcode = new StringBuilder();
                foreach (HtmlElement item in webBrowser1.Document.All)
                {
                    htmlcode.Append( item.InnerHtml);
                }
    
    1. write a javascript code for returning document.documentElement.innerHTML and using InvolkeScript Function To Return Result:
       var htmlcode = webBrowser1.Document.InvokeScript("javascriptcode");
    

提交回复
热议问题