通过jericho包获取meta里的keywords及description的方法一:
// 获取meta里的keywords和description
List segments = source.findAllElements(Tag.META);
getKeywordsDesc(segments);
if (null != segments) {
String keywordsStr = null;
String descriptionStr = null;
int sumSegments = segments.size();
if (sumSegments > 0) {
for (int i = 0; i < sumSegments; i++) {
String metaStr = segments.get(i).toString();
if (null != metaStr && !"".equals(metaStr.trim())) {
// 获取description
if (metaStr.indexOf("description") != -1
&& metaStr.indexOf("content") != -1) {
metaStr = metaStr.replaceAll("\"", "").replaceAll(
"/", "");
descriptionStr = metaStr.substring(metaStr
.indexOf("content"));
descriptionStr = descriptionStr.substring(
descriptionStr.indexOf("=") + 1,
descriptionStr.length() - 1);
descriptionStr = TextHtml.html2text(descriptionStr);
}
parserBean.setDescription(removeTag(descriptionStr));
// 获取keywords
if (metaStr.indexOf("keywords") != -1
&& metaStr.indexOf("content") != -1) {
metaStr = metaStr.replaceAll("\"", "").replaceAll(
"/", "");
keywordsStr = metaStr.substring(metaStr
.indexOf("content"));
keywordsStr = keywordsStr
.substring(keywordsStr.indexOf("=") + 1,
keywordsStr.length() - 1);
keywordsStr = TextHtml.html2text(keywordsStr);
parserBean.setKeywords(removeTag(keywordsStr));
}
}
}// for over
}
}
方法二:
/**
* 获取meta里的keywords和description
*/
private void getKeywordsDesc(List segments) {
if (null != segments) {
String keywords = null;
String description = null;
int sumSegments = segments.size();
for (int i = 0; i < sumSegments; i++) {
String segment = segments.get(i).toString().toLowerCase() ;
if (null != segment && !"".equals(segment.trim())) {
// 获取meta里的keywords
if (segment.indexOf("keywords") > 0
&& segment.indexOf("content") > 0) {
String patternStr = "< *meta *name *= *\"? *keywords *\"? *content *= *\"?(.*) *\"? */? *>";
keywords = Regex(patternStr, segment);
if (null == keywords) {
patternStr = "< *meta *content *= *\"?(.*) *\"? *name *= *\"? *keywords *\"? */? *>";
keywords = Regex(patternStr, segment);
}
if (null != keywords) {
keywords = removeTag(keywords);
}
this.keyowrds = keywords.replace("/", "").replace("\"", "");
}
// 获取meta里的description
if (segment.indexOf("description") > 0
&& segment.indexOf("content") > 0) {
String patternStr = "< *meta name *= *\"? *description *\"? *content *= *\"?(.*) *\"? */? *>";
description = Regex(patternStr, segment);
if (null == description) {
patternStr = "< *meta *content *= *\"?(.*) *\"? *name *= *\"? *description *\"? */? *>";
description = Regex(patternStr, segment);
}
if (null != description) {
description = removeTag(description).replace("/", "");
}
this.description = description.replace("/", "").replace("\"", "");
}
}
}
}
}
private String Regex(String patternStr, String segment) {
String str = null;
Pattern p = Pattern.compile(patternStr,Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(segment);
while (m.find()) {
str = m.group(1);
}
return str;
}
用htmlparser获取meta里面的keywords及descripton
Parser parser=new Parser(url);
NodeFilter filter=new NodeClassFilter(MetaTag.class);
NodeList nodelist=parser.extractAllNodesThatMatch(filter);
for(Node node:nodelist.toNodeArray())
{
MetaTag meta=(MetaTag) node;
System.out.println(meta.getAttribute("name")+":"+meta.getAttribute("content"));
}
分享到:
相关推荐
使用如下钩子即可,但是前提条件是 没有默认配置head的title asyncData ({ app }, callback) { app.head.title = ‘new title' callback(null, {}) ...meta data-n-head=”1″ data-hid=”description” name=”des
本文实例讲述了JS获取多维数组中相同键的值实现方法。分享给大家供大家参考,具体如下: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <TITLE> Demo </...
<meta> 元素可提供有关页面的元信息(meta-information),比如针对搜索引擎和更新频度的描述和关键词。这篇文章给大家介绍了html中meta标签及用法详解,感兴趣的朋友一起看看吧
本文实例讲述了javascript设置页面背景色及背景图片的方法。分享给大家供大家参考,具体如下: <!DOCTYPE HTML PUBLIC -//W3C//DTD HTML 4.0 Transitional//EN> <HTML> <HEAD> <TITLE> New ...
meta name =" keywords " content =" your tags " /> < meta name =" description " content =" 150 words " /> < meta name =" subject " content =" your website's subject " > < meta name =...
在web设计中使用js可以实现很多的页面特效,然而很多人却忽视了HTML标签中META标签的强大功效,其实...meta name="KEYWORDS" content="…">//说明关键词; <meta name="DESCRIPTION" content="…">//说明主
网页设计 <!... <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale... <meta name="keywords" con
本文实例讲述了php获取...meta name="keywords" content="百度,baidu,查询快照结果"> <meta name="description" content="php查询网站快照日期: "> </head> <body> <?php $k=isset($_POST['k']
<!...<head lang=”en”> 标准的 lang 属性写法 <meta charset=’utf-8′>...meta name=”description” content=”不超过150个字符”/> 页面描述 <meta name=”keywords” content=””/>
除了我们常用的定义编码、关键字(name=”keywords”)、描述(name=”description”)(for SEO),还可以定义视区大小、缩放比例等(for 移动端),如下: CSS Code复制内容到剪贴板 <meta name="viewport" content=...
metaname="keywords"content="{$head[keywords]}"> {$head[description]}网页描用法:<metaname="description"content="{$head[description]}"> 指向网站根目录用法:[removed][removed] 广告模块域名...
偶游网网页meta标签优化器,不会做HTML头文件SEO优化的站长可以使用这个!包括了meta中的多项生成,如Description和Keywords等!
DOCTYPE html> <html> <head> <meta name="keywords" content="风舞红枫,前端技术,canvas"/> <meta name="description" content="风舞红枫,前端技术,canvas,vue,react,node,个人博客"/> <meta charset=...
meta name=”keywords” content=”value”>多个关键字内容之间可以用“,”分隔 设置描述:<meta name=”description” content=”value”> 设置作者:<meta name=”author” content=”作者名”> ...
meta name="keywords" content="用dreamweaver当ftp上传网站文件并管理网站的方法_往事如疯_新浪博客,往事如疯,杂谈" /> <meta name="description" content="用dreamweaver当ftp上传网站文件并管理网站的方法_...
<META NAME="Keywords" CONTENT=""> <META NAME="Description" CONTENT=""> body { background: #060e1b; overflow: hidden; } canvas { //opacity: 0.5; } <canvas id="canvas"></canvas> "use...
"Suno AI教程"可能指的是针对Suno ... <meta name="description" content="This is an example of meta tags."> <meta name="keywords" content="HTML, meta tags, metadata"> <meta name="author" content="John Do
这其中最重要的是description(站点在搜索引擎上的描述)和keywords(分类关键词),所以应该给每页加一个meta值。比较常用的有以下几个: name 属性 1、<meta name=Generator contect=>用以说明生成工具(如...
中间人元标记 SEO gem...tags title : 'Relevant title'title 'Relevant title'描述 set_meta_tags description : 'Powerful website full of best practices and keywords'description 'Powerful website full of bes
meta name =" keywords " content =" your, tags "/> < meta name =" description " content =" 150 words "/> < meta name =" subject " content =" your website's subject " > < meta name =...