1

JSOUPを使用してHTMLMETAタグからURL値を抽出するのに助けが必要です。これが私のhtmlです-

String html = "<HTML><HEAD><...></...><META ......><META ......><META http-equiv="refresh" content="1;URL='https://xyz.com/login.html?redirect=www.google.com'"></HEAD></HTML>"

期待される出力: "https://xyz.com/login.html?redirect=www.google.com"

誰かがそれを行う方法を教えてもらえますか?ありがとう

4

2 に答える 2

5

仮定すると、それは最初ですMETA

String html_src = ...

Document doc = Jsoup.parse(html);
Element eMETA = doc.select("META").first();
String content = eMETA.attr("content");
String urlRedirect = content.split(";")[1];
String url = urlRedirect.split("=")[1].replace("'","");
于 2012-10-26T03:38:23.173 に答える
1

Java 8とJsoupを使用すると、このソリューションは機能します。

Document document = Jsoup.parse(yourHTMLString);
String url = document.select("meta[http-equiv=refresh]").stream()
                .findFirst()
                .map(doc -> {
                    try {
                        String content = doc.attr("content").split(";")[1];
                        Pattern pattern = Pattern.compile(".*URL='?(.*)$", Pattern.CASE_INSENSITIVE);
                        Matcher m = pattern.matcher(content);
                        String redirectUrl = m.matches() ? m.group(1) : null;
                        return redirectUrl == null ? null :
                                redirectUrl.endsWith("'") ? redirectUrl.substring(0, redirectUrl.length() - 2) : redirectUrl;
                    } catch (Exception e) {
                        return null;
                    }

                }).orElse(null);
于 2016-01-19T14:52:48.700 に答える