2012-01-13 30 views
2

私はこのようなHTMLコードを持っています。JSoup - メタデータからURLを取得する

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

このHTMLを解析してURL値を取得するには、JSoupを使用します。これどうやってするの?

答えて

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

エキスのみURL部分:

System.out.println(content.split("=")[1]); 
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); 
     } 
} 
関連する問題