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

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

方法一 Http analyzer + httpclient

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

方法二:Selenium+PhantomJS

  • Selenium,PhantomJS是什么?
    Selenium:用于Web应用程序测试的工具
    PhantomJS: PhantomJS是一个基于webkit的javascript api

  • 接下来直接配置项目需要的环境
    1 下载PhantomJS支持windows和linux
    2 将下载的文件解压到指定目录(注我的是 e:/dev)
    3 新建java项目所需的依赖如下:


    
        org.seleniumhq.selenium
        selenium-java
        2.53.0
    

    
    
        com.codeborne
        phantomjsdriver
        1.2.1
    

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;"> 相关的人
                            

-->
法定代表人: 黄金龙
注册资本:6000.000000万人民币 品牌| 相关的人

-->
法定代表人: 赵坤
注册资本:1000万人民币
注册时间:

你可能感兴趣的:(JAVA获取异步(ajax)请求数据)