I have a HTML code which looks like this.
<html><head><meta http-equiv="refresh" content="0;url=http://www.abc.com/event"/></head></html>
I want to use JSoup to parse this HTML and get the url value. How can I do this?
You need to parse the content by yourself. Something like this:
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);
}
}
Parse the input and retrieve the full target text:
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");
Extract only the URL portion:
System.out.println(content.split("=")[1]);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.