How to extract meta tags from website on android?

别来无恙 提交于 2020-01-05 04:10:09

问题


is there a smart way to read the content of metatags from an URL in android? I'll show a webpage in the webview on android and want to read some informations from the metatag inside. Is the only way to parse the string of the webpage to find the special string "meta name="x-..." content="!!!" or is there any smarter way??


回答1:


A smart way would be using Jericho Library

supposing you have an html file like this

<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>

for example to get the value of meta tag with name "joc-height" you can use this method:

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

and you will get the value of "120"



来源:https://stackoverflow.com/questions/15965708/how-to-extract-meta-tags-from-website-on-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!