zoukankan      html  css  js  c++  java
  • java操作html格式数据

    近期在做数据抓取功能,抓取到的数据为html格式,需在后台进行转换后取值,为了避免使用字符串查找方式获取而使用Jsonp完美实现。

    1. 引入Jsonp:

    1 <dependency>
    2     <groupId>org.jsoup</groupId>
    3     <artifactId>jsoup</artifactId>
    4     <version>1.11.2</version>
    5 </dependency>

    2. 进行数据转换:

      2.1 select可以获取HTML标签,类型为Elements;

      2.2 child(int index) 可以根据坐标获取子标签;

      2.3 text()可以获取便签内容。

     1 // 解析返回数据
     2 try {
     3     Document doc = Jsoup.parse(result);
     4     // 获取响应内容指定区域标签
     5     Elements elements = doc.select(".BOC_main").select("tr");
     6     // 获取具体值
     7     text = elements.get(1).child(6).text();
     8 } catch(Exception e) {
     9     e.printStackTrace();
    10 }

    3. 抓取数据方法:

      其中,请求属性要根据实际情况修改。

    private static String getUrlInfo(String url, String methodType, String param){
        try {
            URL url = new URL(urls);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            // 设置连接超时时间
            conn.setConnectTimeout(60000);
            // 设置读取超时时间
            conn.setReadTimeout(60000);
                
            if("Get".equalsIgnoreCase(methodType)) {
                conn.setRequestMethod("GET");
            }else {
                conn.setRequestMethod("POST");
            }
                
            // 设置请求属性
            conn.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
            conn.setRequestProperty("Connection", "keep-alive");
            conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            conn.setRequestProperty("Host", "host");
                
            conn.setDoInput(true);
            conn.setDoOutput(true);
            // 设置是否使用缓存
            conn.setUseCaches(false);
                
            if(StringUtil.isNotBlank(param)) {
                // 建立输入流,向指向的URL传入参数
                DataOutputStream dos=new DataOutputStream(conn.getOutputStream());
                dos.writeBytes(param);
                dos.flush();
                dos.close();
            }
                
            // 输出返回结果
            InputStream input = conn.getInputStream();
            int resLen =0;
            byte[] res = new byte[1024];
            StringBuilder sb=new StringBuilder();
            while((resLen=input.read(res))!=-1){
                sb.append(new String(res, 0, resLen));
            }
            return sb.toString();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }    
  • 相关阅读:
    start pyhton project(2)
    java.lang.ClassFormatError: Truncated class file
    linux 查看计算机信息命令
    VS2010UltimTrialCHS 版注册码
    VS2008打包安装程序,实现覆盖安装设置
    WPF移动不规则渐变色窗体
    C#下移动无边框窗体(直接粘贴可用)
    TCP通信过程中时时监测连接是否已断开
    WIN7下使用DotNetBar,关闭Aero效果,使用Office2007Form皮肤
    【原创】企业级工作流管理系统评价依据或标准
  • 原文地址:https://www.cnblogs.com/commissar-Xia/p/9300000.html
Copyright © 2011-2022 走看看