如何从android网站提取元标签



是否有一种聪明的方法从android的URL中读取元标签的内容?我将在android的webview中显示一个网页,并希望从里面的元标签中读取一些信息。解析网页字符串的唯一方法是找到特殊字符串"meta name="x-…"content="!!"还是有更聪明的方法??

一个聪明的方法是使用Jericho Library

假设你有一个像这样的HTML文件

<html xmlns="http://www.w3.org/1999/xhtml" debug="true">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=Windows-1252"/>
<link href="styleUrgente.css" rel="stylesheet" type="text/css"/>
<meta name="viewport" content="width = 320, initial-scale = 1.0, user-scalable = no"/>
<meta name="joc-height" value="120"/>
<meta name="joc-enabled" value="1"/>
</head>
<body margin="0" marginheight="0" marginwidth="0" topmargin="0" leftmargin="0" rightmargin="0" bottommargin="0">
<script src="chrome-extension://bmagokdooijbeehmkpknfglimnifench/googleChrome.js"/>
</html>
例如,要获取名称为"joc-height"的meta标签的值,可以使用以下方法:
public String extractAllText(String htmlText){
        Source source = new Source(htmlText);   
        String strData = "";        
        List<Element> elements = source.getAllElements("meta");
        for(Element element : elements )
        {
            final String id = element.getAttributeValue("name"); // Get Attribute 'id'
             if( id != null && id.equals("joc-height")){
                 strData = element.getAttributeValue("value").toString();    
                   }
        }
        return strData;
    }
你会得到" 120 "的值

最新更新