Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSoup - getting url from meta data

Tags:

java

jsoup

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?

like image 546
Geek Avatar asked Aug 16 '26 14:08

Geek


1 Answers

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);
        }
}
like image 99
hakyer Avatar answered Aug 18 '26 03:08

hakyer