2
我有一個看起來像這樣的HTML代碼。JSoup - 從元數據中獲取網址
<html><head><meta http-equiv="refresh" content="0;url=http://www.abc.com/event"/></head></html>
我想使用JSoup解析這個HTML並獲得url值。我怎樣才能做到這一點?
我有一個看起來像這樣的HTML代碼。JSoup - 從元數據中獲取網址
<html><head><meta http-equiv="refresh" content="0;url=http://www.abc.com/event"/></head></html>
我想使用JSoup解析這個HTML並獲得url值。我怎樣才能做到這一點?
分析輸入和檢索完整譯文:
Document doc = Jsoup.parse("<html><head><meta http-equiv=\"refresh\" " +
"content=\"0;url=http://www.abc.com/event\"/></head></html>");
String content = doc.getElementsByTag("meta").get(0).attr("content");
只提取URL部分:
System.out.println(content.split("=")[1]);
你需要自己解析內容。事情是這樣的:
Elements refresh = document.head().select("meta[http-equiv=refresh]");
if (!refresh.isEmpty()) {
Element element = refresh.get(0);
String content = element.attr("content");
// split the content here
Pattern pattern = Pattern.compile("^.*URL=(.+)$", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(content);
if (matcher.matches() && matcher.groupCount() > 0) {
String redirectUrl = matcher.group(1);
}
}