简体   繁体   English

JSoup - 从元数据中获取url

[英]JSoup - getting url from meta data

I have a HTML code which looks like this. 我有一个HTML代码,看起来像这样。

<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. 我想使用JSoup来解析这个HTML并获取url值。 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: 仅提取URL部分:

System.out.println(content.split("=")[1]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM