分享

html parser

 昵称1773864 2011-03-21
        //下面是htmlparser查找余额方法
//        String html = this.postValuesFor3yx(LOGIN,HYZX,userName,passWord);
//        Parser parser=Parser.createParser(html, "gbk");
//        NodeFilter filter=new AndFilter(new TagNameFilter("span"), new HasAttributeFilter("class", "bold cor_3366CC"));
//        List<String> remainingMoneys=new ArrayList<String>();
//        try {
//            NodeList nodelist=parser.extractAllNodesThatMatch(filter);
//            Node[] nodes = nodelist.toNodeArray();
//            for (Node node : nodes) {
//                remainingMoneys.add(node.getChildren().elementAt(0).toHtml());
//            }
//            return remainingMoneys.size()>0?remainingMoneys.get(0):null;
//        } catch (ParserException e) {
//            e.printStackTrace();
//        }
//        return null;

//正则表达式
//        String html = this.postValuesFor3yx(LOGIN,HYZX,userName,passWord);
//        String extractRegText = "";
//        String userWalletAmount = "";
//        try {
//            extractRegText = extractRegText(html, "可用余额:.+?元");
//            //System.out.println(extractRegText);
//            userWalletAmount = extractRegText(extractRegText, ">.+?<");
//            userWalletAmount = extractRegText(userWalletAmount,"\\d+(.\\d{2})?");
//            return new String(userWalletAmount);
//        } catch (MalformedPatternException e1) {
//            e1.printStackTrace();
//        }
//        return null;

public static String extractRegText(String source, String regxp) throws MalformedPatternException {
        if (source == null) {
           return "";
        }
        Pattern pattern = new Perl5Compiler().compile(regxp, Perl5Compiler.READ_ONLY_MASK);
        Perl5Matcher matcher = new Perl5Matcher();
        if (matcher.contains(source, pattern)) {
            return matcher.getMatch().toString();
        }
        return "";
        }

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多