Capturing JS Dynamic Data generation

Source: Internet
Author: User

Recently in the grasp of data, general Web data capture is relatively easy, today in the TV cat's program, found that some data can not be crawled, the Java end of the HTML file does not have a piece of code, looked up a lot of information, found that JS Dynamic Data generated, can not be directly crawled, There is a workaround is to use to find the AJAX request address and parameters, re-fetch, the method has a problem, that is, when the parameters are encrypted obsolete, the method is not good use, so, here is a way to use Htmlunit to crawl (can use JSUOP to handle the HTML file), Jar Package: http://download.csdn.net/detail/joy460154849/9822849 (the jar package used by htmlunit all imported, slightly adjusted, because some have duplicate import or version problem) code is as follows:

 Public Staticstring gethtml (string url) {//Simulate a browser@SuppressWarnings ("Resource") WebClient WebClient=NewWebClient (); //webclient parameter carrierWebclientoptions clientoptions =webclient.getoptions (); //set parameters for WebClientClientoptions.setjavascriptenabled (true); Clientoptions.setcssenabled (false); Webclient.setajaxcontroller (NewNicelyresynchronizingajaxcontroller ()); Clientoptions.settimeout (35000); Clientoptions.setthrowexceptiononscripterror (false); //simulate browser to open a destination URLHtmlPage Rootpage =NULL; Try{rootpage=webclient.getpage (HTML); } Catch(failinghttpstatuscodeexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(malformedurlexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }            //Save page//Rootpage.save (New File ("/HOME/SHARE/TEST/1")); //Body HTML InformationHtmlElement HtmlElement =Rootpage.getbody (); String xmlcontent=Htmlelement.asxml (); returnxmlcontent; }  

Capturing JS Dynamic Data generation

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.