需要帮助使用JSOUP从HTML META标记中提取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”
任何人都可以告诉我该怎么做。感谢
答案 0 :(得分:4)
假设,这是第一个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("'","");
答案 1 :(得分:0)
使用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);