美文网首页我爱编程
JAVA获取异步(ajax)请求数据

JAVA获取异步(ajax)请求数据

作者: Vincilii | 来源:发表于2017-03-11 13:34 被阅读0次

    前言
    2016年大四上学期实习,参与爬虫系统开发,公司也遇到了如何获取ajax,vue,angularJS渲染的网页的问题,于是我便开始在Baidu,Google上寻觅了几天,获得了以下两种较为稳定方法。

    方法一 Http analyzer + httpclient

    获取一个网站某个数据区域的精准的数据,那么推荐用Http analyzer分析请求及参数,用httpclient模拟请求(注意如果是多个请求,那么必须用同一个httpclient对象去执行,因为高版本的HttpClient会自动保持Cookie信息),一般都能够获得请求返回的json数据,当然,如果网站有做反爬虫处理或者其他的一些处理,那就困难了。

    方法二:Selenium+PhantomJS

    <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>2.53.0</version>
        </dependency>
    
        <!-- https://mvnrepository.com/artifact/com.codeborne/phantomjsdriver -->
        <dependency>
            <groupId>com.codeborne</groupId>
            <artifactId>phantomjsdriver</artifactId>
            <version>1.2.1</version>
        </dependency>
    

    4 编码,为了进行对比获取ajax数据差异,使用httpclient与之对比
    Selenium+PhantomJS:

    public class WebdriverDownloader {
        static {
           //phantomJsPath=E:/dev/phantomjs/bin/phantomjs.exe
            String phantomJsPath = PropertyResourceBundle.getBundle("webdriver").getString("phantomJsPath");
               System.setProperty("phantomjs.binary.path",phantomJsPath);
        }
        /**
         * download html
         * @param webDriver
         * @param url
         * @return
         */
        public static String download(WebDriver webDriver,String url){
            webDriver.get(url);
            WebElement webElement = webDriver.findElement(By.xpath("/html"));
            return webElement.getAttribute("outerHTML");
        }
        public static WebDriver create(){
            WebDriver webDriver=new PhantomJSDriver();
            /**Specifies the amount of time the driver should wait when searching for an element if it is
             * not immediately present.*/
            webDriver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
            return webDriver;
        }
    
        /**
         *
         * close window,must quit first,then close
         */
        public static void close(WebDriver driver)  {
            driver.quit();
            driver.close();
        }
         public static void main(String []args){
             String url = "http://www.tianyancha.com/search?key=%E7%99%BE%E5%BA%A6&checkFrom=searchBox";
             WebDriver webDriver = WebdriverDownloader.create();
            String html = WebdriverDownloader.download(webDriver, url);
            // WebdriverDownloader.close(webDriver);
             System.out.println(html);
         }
    

    HttpClient 代码:

    public class HttpDownload {
        public static String  download(String url) throws IOException {
            CloseableHttpClient client = HttpClients.createDefault();
           return EntityUtils.toString(client.execute(new HttpGet(url)).getEntity());
        }
    
        public static void main(String []args) throws IOException {
            String url = "http://www.tianyancha.com/search?key=%E7%99%BE%E5%BA%A6&checkFrom=searchBox";
            String html = HttpDownload.download(url);
            System.out.println(html);
        }
    }```
    
    Selenium+PhantomJS:
    由于源码过长,截取部分源码和HttpClient下载的源码做对比
    
                                          style="margin-right: 2px;"> 相关的人</span><img
                                        style="height: 14px; margin-bottom: 3px;"
                                        src="http://static.tianyancha.com/wap/images/cutting-line.png" /><span
                                        style="margin-left: 5px;" class="search_last_color"
                                        ng-bind-html="node.humanNames"></span></span>
                                </p> --></div><!--body--><div class="row" style="margin-left: 0; margin-right: 0"><div class="search_row_new"><div class="title overflow-width" style="padding-left: 0">法定代表人: <span title="黄金龙" ng-bind-html="node.legalPersonName?node.legalPersonName:'未公开' | trustHtml" class="ng-binding">黄金龙</span></div><!-- <div class="title overflow-width">
                                        行业:<span title="{{node.industry?node.industry:'未公开'}}">{{node.industry?node.industry:'未公开'}}</span>
                                    </div> --><div class="title overflow-width">注册资本:<span title="6000.000000万人民币" class="ng-binding">6000.000000万人民币
                                <span ng-if="node.trademarks" style="margin-right: 20px;"><span
                                        class="c3" style="margin-right: 2px;">品牌</span><img alt="|"
                                                                                            style="height: 14px; margin-bottom: 3px;"
                                                                                            src="http://static.tianyancha.com/wap/images/cutting-line.png" /><span
                                        style="margin-left: 5px;" class="search_last_color"
                                        ng-bind-html="node.trademarks | trustHtml"></span></span> <span
                                        ng-if="node.humanNames"><span class="c3"
                                                                      style="margin-right: 2px;"> 相关的人</span><img
                                        style="height: 14px; margin-bottom: 3px;"
                                        src="http://static.tianyancha.com/wap/images/cutting-line.png" /><span
                                        style="margin-left: 5px;" class="search_last_color"
                                        ng-bind-html="node.humanNames"></span></span>
                                </p> --></div><!--body--><div class="row" style="margin-left: 0; margin-right: 0"><div class="search_row_new"><div class="title overflow-width" style="padding-left: 0">法定代表人: <span title="赵坤" ng-bind-html="node.legalPersonName?node.legalPersonName:'未公开' | trustHtml" class="ng-binding">赵坤</span></div><!-- <div class="title overflow-width">
                                        行业:<span title="{{node.industry?node.industry:'未公开'}}">{{node.industry?node.industry:'未公开'}}</span>
                                    </div> --><div class="title overflow-width">注册资本:<span title="1000万人民币" class="ng-binding">1000万人民币</span></div><div class="title overflow-width" style="border-right: none">注册时间:<span title="2013-10-23 " clnode.bondType --><!-- <p ng-if="node.humanNames||node.trademarks">
                                        ```
    

    可以看到Selenium+PhantomJS可以获取到动态邦定的数据,在源码中可以看到ng-binding,说明网站前端使用了angurlarJS。当然获取到了源码提取出来的信息才是有的,一般都可以使用xpath(使用xpath推荐使用xpath heper这个chrome浏览器插件可以帮助提高写xpath的效率),jsoup这些进行源码的解析,此文不再赘述。

    总结:
    直接用Java获取ajax或者使用vue.js,angularJS进行前端渲染的页面往往得不到渲染的网页,因为这些网页都是需要执行JS进行渲染的。
    使用HtmlUnit执行JS易出错,我用的时候基本没成功过,只有Selenium+浏览器的方案是目前相对稳妥的,但是ChromeDriver和FireFoxDriver在启动时都会弹出一个对话框,这样的体验并不好,而PhantomJS却可以在后台运行。
    网上Java使用PhantomJS组合方案时一般都会写一段js让PhantomJS去load网页,其实使用Selenium+PhantomJS那段js就不用写了。并且还可以使用Seleinum提供的API进行模拟点击。
    注意:上面的代码仅用于测试,如果要投入生产需要考虑很多,在创建Webdriver的过程是一个非常耗时的过程,根据使用场合可以将Webdriver进行池化(可以使用apache的Commons Pool
    注:实习结束了,也没做爬虫了,写个文档纪念一下。文中有很多参考了别人的博客,但是时间太久记不住是哪些博客了,敬请见谅。如果观点有误,欢迎指正。

    相关文章

      网友评论

        本文标题:JAVA获取异步(ajax)请求数据

        本文链接:https://www.haomeiwen.com/subject/gjxngttx.html