簡體   English   中英

JSoup - 從元數據中獲取url

[英]JSoup - getting url from meta data

我有一個HTML代碼,看起來像這樣。

<html><head><meta http-equiv="refresh" content="0;url=http://www.abc.com/event"/></head></html>

我想使用JSoup來解析這個HTML並獲取url值。 我怎樣才能做到這一點?

您需要自己解析內容。 像這樣的東西:

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);
        }
}

解析輸入並檢索完整的目標文本:

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]);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM