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;
}
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?
From your code,
This is not correct. This is merely selecting
while you actually want to get the attribute
You need to use
attr("content")instead ofselect("content").See also:
SelectorAPI documentationUnrelated to the concrete problem, you don’t need to test against a
booleaninside anifblock. TheisEmpty()already returns aboolean: