Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find a specific meta tag

I am trying to retrieve a meta tag (Tag name=Generator) using Jsoup parser in java.

The code I have is given below:

Elements metalinks=doc.select("meta"); // meta

boolean metafound=false;
for (Element singlemeta : metalinks) 
{
    metatagname = singlemeta.attr("abs:name");
    metatagcontent = singlemeta.attr("abs:content");
    if((metatagname=="Generator")||(metatagname=="generator")||(metatagname=="GENERATOR")){
        //  this is the tag we want to get value of...
        metarequired=metatagcontent;
        metafound=true;
    }
}   
if(metafound==false)
    metarequired="NOT_FOUND";

However I am unable to extract the meta GENERATOR tag correctly.

One example of this tag is now given below:

<meta name="generator" content="Test page" />

For the very first line in code given above, I also tried the following code but that also does not work:

//Elements metalinks= doc.getElementsByTag("meta");

How do I extract the meta tag correctly?

like image 789
Arvind Avatar asked Dec 07 '22 18:12

Arvind


1 Answers

It almost looks as if you're making it too complicated. What if you started out with something simple like this:

Elements metalinks = doc.select("meta[name=generator]");
like image 156
Hovercraft Full Of Eels Avatar answered Jan 04 '23 07:01

Hovercraft Full Of Eels