C # capture js dynamically generated HTML tools: NHtmlUnit Extract

Source: Internet
Author: User


(This blog post is purely a personal record. Please indicate the source for reprinting !)

Official Address:NHtmlUnit

Dll compilation method:

1. Download nuget.exe (nuget Website: http://www.nuget.org /)

2. Open the doscommand dialog box,

Directory where the nuget.exe file is located (My exe file is placed on the desktop)

 


3. Install NHtmlUnit. After installation, two folders will be created in the current directory. One is to install the required environment IKVM, and the other is the required NHtmlUnit folder, which contains the generated dll under the lib directory.

Run the command: nuget install NHtmlUnit


 

3. You can go to the official HtmlUnit website to read the document or search for tutorials in the blog Garden. The usage of HtmlUnit is the same as that of JAVA. NHtmlUnit is only

Add a shell to HtmlUnit so that. net can call JAVA.

 

Instance code (reference dll: HtmlUnit, NHtmlUnit. To be lazy, I have referenced all the IKVM dll files ):

class Program
    {
        static void Main(string[] args)
        {
            WebClient client = new WebClient();
            client.Options.JavaScriptEnabled = false;
            client.Options.CssEnabled = true;
            client.Options.ThrowExceptionOnScriptError = false;
            client.Options.Timeout = 5000;
            HtmlPage page = client.GetHtmlPage("http://www.baidu.com/#wd=11&rsv_spt=1&issp=1&rsv_bp=0&ie=utf-8&tn=baiduhome_pg&rsv_sug3=2&rsv_sug2=0&inputT=26");
            Thread.Sleep(3000);
            string xml = page.AsXml();
            HtmlDocument doc = new HtmlDocument();//忽略此类,这个类是其他DLL提供的,与NHtmlUnit无关
            doc.LoadHtml(xml);
            string nodeName = doc.GetElementbyId("container").Name;
            Console.WriteLine("\r\n\r\n\r\n\r\n" + nodeName + "\r\n\r\n\r\n\r\n" );
            Console.Read();
        }
    } 


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.