zoukankan      html  css  js  c++  java
  • Java-如何挖取某个网站中的ajax请求信息

    通常情况,通过网络爬虫挖取到的基本为网页静态内容,而动态ajax取数的内容是我个人暂时不知如何一次性把网站中的ajax获取

    这里介绍的是某个网站中的某一个ajax多某个table刷新,期数据,并提供其他操作,如下载:

    假设我们需挖取某一个网站:

    例:某个网站中的那些pdf文件,并下载下来

    首先:需要分析期网页组成结果;查看是通过什么方式读取处理的。这里结束ajax的方案(其它异同,ajax只是对数据进行一个一次数据的请求)

    具体操作已案例介绍为主:

    首先分析需要使用到的是ajax使用的请求url和请求中所需要的参数的含义,然后给定响应的参数

        /**
         * 获取某个请求的内容
         * @param url  请求的地址
         * @param code 请求的编码,不传就代表UTF-8
         * @return 请求响应的内容
         * @throws IOException
         */
        public static String fetch_url(String url, String code) throws IOException {
            BufferedReader bis = null; 
            InputStream is = null; 
            InputStreamReader inputStreamReader = null;
            try { 
                URLConnection connection = new URL(url).openConnection(); 
                connection.setConnectTimeout(20000);
                connection.setReadTimeout(20000);
                connection.setUseCaches(false);
                connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11");
                is = connection.getInputStream(); 
                inputStreamReader = new InputStreamReader(is, code);
                bis = new BufferedReader(inputStreamReader); 
                String line = null; 
                StringBuffer result = new StringBuffer(); 
                while ((line = bis.readLine()) != null) { 
                    result.append(line); 
                } 
                return result.toString(); 
            } finally { 
                if (inputStreamReader != null) {
                    try { 
                        inputStreamReader.close();
                    } catch (IOException e) { 
                        e.printStackTrace(); 
                    } 
                }
                if (bis != null) { 
                    try { 
                        bis.close(); 
                    } catch (IOException e) { 
                        e.printStackTrace(); 
                    } 
                } 
                if (is != null) { 
                    try { 
                        is.close(); 
                   } catch (IOException e) { 
                        e.printStackTrace(); 
                    } 
                } 
            } 
        }

    通过上面的url请求,观看响应的数据格式,这里响应的数据测试格式为json格式

        /**
         * 数据转化成json格式
         * @param s
         */
        public static void getJSON(String s) {
            JSONObject object = JSONObject.fromObject(s);
            JSONArray array = JSONArray.fromObject(object.get("disclosureInfos"));
            //System.out.println(array);
            String filePath = object.getString("filePath");//解析数据中的某一个值
            //System.out.println(array.size());
            List<String> listFilePath = getJSONArray(array,filePath);//将数据解析成条数
            /*System.out.println(listFilePath);
            System.out.println(listFilePath.size());*/
            writer(listFilePath);//根据数据的内容开始挖取下载
        }

    很多个数据需要下载,一条一条的下载

        public static void writer(List<String> listFilePath) {
            for (String string : listFilePath) {
                downloadFile(string);
            }
        }

    解析json数据从格式数据

        /**
         * 解析文件url
         * @param array
         * @return
         */
        public static List<String> getJSONArray(JSONArray array,String filePath) {
            List<String> listFilePath = new ArrayList<String>();
            for (Object object : array) {
                JSONObject ob = JSONObject.fromObject(object);
                filePath = filePath + ob.get("filePath").toString();
                // System.out.println(filePath);
                listFilePath.add(filePath);
            }
            return listFilePath;
        }

    文件下载处理

        /* 下载 url 指向的网页 */
        public static String downloadFile(String url) {
            String filePath = null;
            /* 1.生成 HttpClinet 对象并设置参数 */
            HttpClient httpClient = new HttpClient();
            // 设置 Http 连接超时 5s
            httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(5000);
            /* 2.生成 GetMethod 对象并设置参数 */
            GetMethod getMethod = new GetMethod(url);
            // 设置 get 请求超时 5s
            getMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, 5000);
            // 设置请求重试处理
            getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());
            /* 3.执行 HTTP GET 请求 */
            try {
                int statusCode = httpClient.executeMethod(getMethod);
                // 判断访问的状态码
                if (statusCode != HttpStatus.SC_OK) {
                    System.err.println("Method failed: " + getMethod.getStatusLine());
                    filePath = null;
                }
                /* 4.处理 HTTP 响应内容 */
                byte[] responseBody = getMethod.getResponseBody();// 读取为字节数组
                // 根据网页 url 生成保存时的文件名
                filePath = "e:\spider\";
                String fileName =  getFileNameByUrl(url, getMethod.getResponseHeader("Content-Type").getValue());
                saveToLocal(responseBody, filePath,fileName);
            } catch (HttpException e) {
                // 发生致命的异常,可能是协议不对或者返回的内容有问题
                System.out.println("Please check your provided http address!");
                e.printStackTrace();
            } catch (IOException e) {
                // 发生网络异常
                e.printStackTrace();
            } finally {
                // 释放连接
                getMethod.releaseConnection();
            }
            return filePath;
        }

    确认文件名称和文件格式

        /**
         * 根据 url 和网页类型生成需要保存的网页的文件名 去除掉 url 中非文件名字符
         */
        public static String getFileNameByUrl(String url, String contentType) {
            // remove http://
            url = url.substring(7);
            // text/html类型
            if (contentType.indexOf("html") != -1) {
                url = url.replaceAll("[\?/:*|<>"]", "_") + ".html";
                return url;
            }
            // 如application/pdf类型
            else {
                return url.replaceAll("[\?/:*|<>"]", "_") + "." + contentType.substring(contentType.lastIndexOf("/") + 1);
            }
        }

    保存文件地址写入

        /**
         * 保存网页字节数组到本地文件 filePath 为要保存的文件的相对地址
         */
        private static void saveToLocal(byte[] data, String fileDir,String fileName) {
            try {
                File fileNew=new File(fileDir+"\"+fileName);//new 一个文件 构造参数是字符串
                File rootFile=fileNew.getParentFile();//得到父文件夹
                
                if( !fileNew.exists()) {
                    rootFile.mkdirs();
                    fileNew.createNewFile();
                }
                DataOutputStream out = new DataOutputStream(new FileOutputStream(fileNew));
                for (int i = 0; i < data.length; i++)
                    out.write(data[i]);
                out.flush();
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    测试,这里写了个测试网址,网站地址和网址参数可能会改变,需适当调整

        
        public static void main(String[] args)  throws Exception{
            String s = fetch_url("http://www.neeq.cc/controller/GetDisclosureannouncementPage?type=7&key=&startDate=2015-05-20&endDate=2015-05-21&queryParams=0&page=1&_=1432187131769", "utf-8");
            //System.out.println(s);
            getJSON(s);
        }
        

    完(欢迎转载)

  • 相关阅读:
    ACL最大权限及相关命令
    ACL权限设置
    Linux权限管理--ACL权限简介
    Linux用户信息文件/etc/passwd
    本地光盘yum源的搭建
    响应式 和 移动 web
    svg
    vi 命令
    html 语法
    运营给的广告设计稿如何做
  • 原文地址:https://www.cnblogs.com/hwaggLee/p/4520048.html
Copyright © 2011-2022 走看看