我有一个看起来像这样的HTML代码。
<html><head><meta http-equiv="refresh" content="0;url=http://www.abc.com/event"/></head></html>
我想使用JSoup来解析此HTML并获取url值。我怎么能这样做?
答案 0 :(得分:5)
您需要自己解析内容。像这样:
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);
}
}
答案 1 :(得分:3)
解析输入并检索完整的目标文本:
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");
仅提取网址部分:
System.out.println(content.split("=")[1]);