如何提取meta name = generator标签的content属性? [英] How to extract the content attribute of the meta name=generator tag?
问题描述
我正在使用以下代码使用Jsoup从网页中提取元生成器"标签内容:
I am using the below code to extract meta 'generator' tag content from a web page using Jsoup:
Elements metalinks = doc.select("meta[name=generator]");
boolean metafound=false;
if(metalinks.isEmpty()==false)
{
metatagcontent = metalinks.first().select("content").toString();
metarequired=metatagcontent;
metafound=true;
}
else
{
metarequired="NOT_FOUND";
metafound=false;
}
问题在于,对于包含meta生成器标签的页面,没有显示任何值(当我输出变量'metarequired'的值时.对于没有meta生成器标签的页面,则值为'NOT_FOUND'正确显示了.我在这里做什么错了?
The problem is that for a page that does contain the meta generator tag, no value is shown (when I output the value of variable 'metarequired'. For a page that does not have meta generator tag, the value 'NOT_FOUND' is shown correctly. What am I doing wrong here?
推荐答案
从您的代码中
metalinks.first().select("content").toString();
这是不正确的.这只是选择
This is not correct. This is merely selecting
<meta ...>
<content ... /> <!-- This one, which of course doesn't exist. -->
</meta>
实际上 要获取属性
<meta ... content="..." />
您需要使用 attr("content")
代替 select("content")
.
You need to use attr("content")
instead of select("content")
.
metatagcontent = metalinks.first().attr("content");
另请参见:
- Jsoup食谱-选择器语法
- Jsoup
Selector
API文档 - W3 CSS3选择器规范
- Jsoup cookbook - Selector syntax
- Jsoup
Selector
API documentation - W3 CSS3 selector specification
See also:
无关与具体问题无关,您无需针对if
块内的boolean
进行测试. isEmpty()
已经已返回boolean
:
Unrelated to the concrete problem, you don't need to test against a boolean
inside an if
block. The isEmpty()
already returns a boolean
:
if (!metalinks.isEmpty())
这篇关于如何提取meta name = generator标签的content属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!