Java crawls dynamically generated Web pages

Source: Internet
Author: User

Recently in the project, there is a need to crawl data from the Web page, the requirement is to first crawl the entire Web page of the HTML source (later update to use). Just beginning to look at this simple, and then splinters the code (the use of the Hadoop platform before the Distributed Crawler Framework Nutch, it is very convenient to use, but in the end because of the speed of the reasons to give up, but the resulting statistics are used in the subsequent crawl), Soon holder.html and finance.html page successful download completed, and then parse the holder.html page after parsing finance.html, and then very frustrated found in this page I need the data is not in the HTML source, then go to the browser to view the source code is really like this, in the source The code does not have the data I need, it seems that I did not write the program wrong, and then let the body and mind tired things come---get HTML pages that contain dynamic content.

  in the so-called China's strongest search engine---Baidu walked a long time, found that most people will use Webdriver and httpunit (in fact, the former has included the latter), this happy, finally found a solution. With a very exciting use of webdriver, I want to curse.   The following is about the webdriver of the Groove   Webdriver is a test framework, originally designed not to serve the crawler, but I want to say is: Eight is a short, you can not go forward to do one more step? Why are there so many people on the internet recommending Webdriver? I think these people did not start from the actual, and even some people raving Webdriver can parse the finished page back to the person who wants to crawl the entire page (including dynamically generated content), yes, Webdriver can do this task, but see the author's code, I want to say: Dude, Your code is too restrictive, parse your own JS code, and the JS code is simple, so webdriver of course is no pressure to complete the task. Webdriver in parsing dynamic content is to look at the complexity and diversity of JS code.   What is complexity?   Paste a code First  webdriver driver = Newinternetexplorerdriver (); HtmlPage page = driver.get (URL); System.out.println (Page.asxml ()); This piece of code means to believe that we all understand, above the use of the IE kernel, of course, there are firefoxdriver, Chromedriver,htmlunitdriver, The use of these driver is the same principle, first open the browser (this time), and then load the URL and complete the dynamic parsing, and then through the Page.asxml () can be completed by the HTML page, where Htmlunitdriver simulation interface browser, Java has the implementation of JS engine Rhino,htmlunitdriver use is rhino to parse JS, because not to start the interface browser, so htmlunitdriver faster than the previous three. No matter what driver, can not avoid is parsing JS, it takes time, and not the kernel of the JS support program is different, for example, htmlunitdriver with a scrolling JS code support is very poor, in the execution will be error (personally experience). The complicated meaning of JS code is: for different kernels they support the JS is not exactly the same, this should be based on the specific situationTo set, I have not studied JS for a long time, so about the core of the JS support is not said.   What is diversity   said before, browser parsing JS takes time. For pages that embed only a handful of JS code, there is no problem with page.asxml () to get the full page. But for a page that embeds more JS code, parsing JS takes a lot of time (for the JVM), so at this point the page that is fetched by page.asxml () does not contain dynamically generated content. The question is, so why do you say Webdriver can get an HTML page with dynamic content? Some people on the internet say that after driver.get (URL), it is necessary for the current thread to wait to get the finished page, which is similar to the following form    webdriver driver = new Internetexplorerdriver () ; HtmlPage page = dirver.get (URL); Thread.Sleep (2000); System.output.println (Page.asxml ()); I follow this idea to try the following, yes, really can. But isn't the problem just right there? How to determine the waiting time? Similar to the empirical approach to determining thresholds in data mining? , or as long as it takes. I think these are not very good methods, the time cost is relatively large. I just want to driver should be able to capture the parsing JS after the completion of the state, so I went to find Ah, ah, but there is no such method, so I said webdriver the designer why no longer move forward, let us in the program to get to driver resolution JS after the completion of the state, This will not use Thread.Sleep (2000) Such a uncertainty code, unfortunately, how can not find, really let me heartache a. Firefoxdriver, Chromedriver,htmlunitdriver also have the same problem, it can be said that using Webdriver to help crawl the dynamically generated Web page results are very unstable. This point I have deep experience, when using Iedriver, the same page two times crawl results will appear different, and even sometimes ie directly hanging off, you say such things you dare to use in the crawler? I'm afraid of it.   There is someone recommend the use of httpunit, in fact, Webdirver htmlunitdriver in the internal use is httpunit, so use HttpUnit will encounter the same problem, I also did the experiment, it is true.Through Thread.Sleep (2000) to wait for the completion of the parsing of JS, I think the method is not advisable. Uncertainty is too great, especially in large-scale gripping work.   Summary, Webdriver is designed for testing framework, although in accordance with its principle can be used to assist the crawler to get the HTML page containing dynamic content, but in the actual application is not taken, the uncertainty is too big, the stability is too poor, the speed is too slow, Let's get the framework to do its best, and don't break the pros.   My work is not finished, so continue to find a way online, this time found a stable, high certainty of the auxiliary tool---phantomjs, I do not fully understand this thing. But it has been used to achieve the functions I want. In Java through Runtime.exec (ARG) to call Phantomjs get parsing JS after the page. I'm going to stick the code out.   PHANTOMJS the code to execute   Copy code system = require (' system ')   address = system.args[1];// The second parameter of the command line is then used for   //console.log (' Loading a Web page ');   var page = require (' webpage '). Create ();   var url = address;   //console.log (URL);   page.open (URL, function (status) {     //page is loaded!       if (s Tatus!== ' success ') {          Console.log (' Unable to post! ');      } else {      //Print here is the result of first-class output in Java, Java through InputStream can get the output         Console.log (PagE.content);                phantom.exit ();   });     Copy code java-Side Execution code     copy Code public void getparseredhtml () {  String url = "www.bai.com";  Runtime runtime = Runtime.getruntime ();  runtime.exec ("F:/phantomjs/phantomjs/phantomjs.exe f:/js/parser.js" + URL);  InputStream in = Runtime.getinputstream (); //After the code omitted, got InputStream to say     } Copy the code this way the Java side will be able to get the parsed HTML page, instead of using the Thread.Sleep () code like Webdriver to get the code that might be done. There is a point to note: In the PHANTOMJS side of the JS code must not be grammatical errors, or JS code compiled differently, Java side has been waiting, will not throw an exception. And then because in the use of Phantomjs.exe, the Java side each time to open a PHANTOMJS process, time consumption or relatively large. But at the very least, the results are stable. Of course, finally I have not used phantomjs, I directly download need data, and did not go to crawl the entire page, mainly the speed of the problem (in fact, I dare not use is because PHANTOMJS unfamiliar, so I use caution).   Toss for a few days, although did not solve my problem, but a lot of insight long, late work is familiar with PHANTOMJS, see can be improved speed, if you can break the speed of the box, and then crawl to the web when it is handy, and then nutch this framework, I admire my buddies. When using the convenience, all late is necessary to study how to optimize nutch on Hadoop crawl speed, in addition, Nutch original function will not crawl dynamically generated page content, but can use Nutch and webdirver combination, Maybe the crawl results are stable, haha, these are just ideas, butis not trying how to know it?   If the park friends have to say about the stability of the results obtained by using webdriver-assisted crawlers, welcome everyone, because I did not find the relevant information to stabilize the results.

Java crawls dynamically generated Web pages

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.