您的位置:首页 > 编程语言 > Java开发

Java实现正则匹配出script标签中字符串包含某个几个单词的所有行,以及编码转换

2017-03-24 16:53 381 查看
/*取得script下面的JS变量*/  

        Elements scriptinfo = document.getElementsByTag("script").eq(21);
 

        List<String> list=new ArrayList<String>();

        Map<String,String> PopWindowInfo = new LinkedHashMap<String,String>();

        /*循环遍历script下面的JS变量*/  

        for (Element script : scriptinfo) {  

            

            Pattern p=Pattern.compile("((impression|more_desc|address|phone:)"+".+?)(\",)");

            Pattern p2=Pattern.compile("((ticket_info|bestvisittime|besttime|open_time_desc)"+".+?);");             

            

            String sss=script.data().toString();

//            new JsonPathSelector("$..title").select(page.getRawText());

            

             System.out.println(sss);  

             Matcher m = p.matcher(sss);//与字符串匹配

             String arrq=null;

             while(m.find()){  

                 if(m.group(1).split(":\"").length>1){

                     arrq= m.group(1).split(":\"")[1];

                 }else{

                     arrq="";

                 }

                 PopWindowInfo.put(m.group(1).split(":\"")[0], convert(arrq));

                

                 // System.out.println(m.group(1).split(":\"")[0]+":"+new String(arrq.getBytes("ISO8859-1"), "GBK"));

                System.out.println(m.group(1).split(":\"")[0]+":"+ convert(arrq));  

                

        }

                             

             Matcher m2 = p2.matcher(sss);//与字符串匹配

             String arrqq=null;

             String [] stt = null;

             while(m2.find()){

                 String st="\',\\{text:\"";

                 if(m2.group(1).split(st).length>1){

//                     System.out.println(m2.group(1).split(st)[1]);

                     stt= m2.group(1).split(st)[1].split("\"}\\)");

                                 

                 if(stt.length !=0){

                     arrqq =stt[0];

                 }else{

                     arrqq="";

                 }                

                 PopWindowInfo.put(m2.group(1).split(st)[0],convert(arrqq));
//                System.out.println(m.group(1).split(":\"")[0]+":"+new String(arrq.getBytes("ISO8859-1"), "GBK"));

                System.out.println(m2.group(1).split(st)[0]+":"+convert(arrqq));

        }

                

        }
 }

public static String convert(String utfString){  

           StringBuilder sb = new StringBuilder();  

           int i = -1;  

           int pos = 0;  

             

           while((i=utfString.indexOf("\\u", pos)) != -1){  

               sb.append(utfString.substring(pos, i));  

               if(i+5 < utfString.length()){  

                   pos = i+6;  

                   sb.append((char)Integer.parseInt(utfString.substring(i+2, i+6), 16));  

               }  

           }  

             

           return sb.toString();  

        }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐