如何提取网页的meta标签在Android? [英] How to extract meta tags from website on android?

查看:217
本文介绍了如何提取网页的meta标签在Android?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有读取元标记从一个android的URL的内容一个聪明的方式?我将展示在Android的WebView中的网页,并希望从阅读里面的元标记一些信息。是解析网页的字符串,找到特殊的字符串META NAME =X -...的唯一途径CONTENT =!!!或有任何更聪明的方法??

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

推荐答案

一个聪明的办法将使用杰里科图书馆

A smart way would be using Jericho Library

假设你有一个这样的html文件,

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>

例如获得与名称meta标签的值 JOC高度,你可以用这个方法:

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

,你会得到的值 120

这篇关于如何提取网页的meta标签在Android?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆