[英]Parsing <META content=“”> using jsoup
需要幫助使用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”
任何人都可以告訴我該怎么做。 謝謝
假設,它是第一個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("'","");
使用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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.