ThatGuy343
ThatGuy343

Reputation: 2424

Java get string list from html with JSoup

I need to get the content of the meta tag with the name "keywords" from a URL.

<meta name="keywords" content="cat,dog,woof,meow">

How can i do this with JSoup?

I have tried getting the element by class, then trying to get the content if the name was keywords, but had no luck:

String keywords = document.select("meta.[name=keywords]").get(0).attr("content");

I don't know what I'm doing when it comes to an element without an ID, the error given is pretty straightforward:

Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: String must not be empty
    at org.jsoup.helper.Validate.notEmpty(Validate.java:92)
    at org.jsoup.select.QueryParser.byClass(QueryParser.java:208)
    at org.jsoup.select.QueryParser.findElements(QueryParser.java:146)
    at org.jsoup.select.QueryParser.parse(QueryParser.java:65)
    at org.jsoup.select.QueryParser.parse(QueryParser.java:39)
    at org.jsoup.select.Selector.<init>(Selector.java:80)
    at org.jsoup.select.Selector.select(Selector.java:93)
    at org.jsoup.nodes.Element.select(Element.java:252)

Upvotes: 4

Views: 1300

Answers (1)

Nile
Nile

Reputation: 396

change

document.select("meta.[name=keywords]") 

to

document.select("meta[name=keywords]")

http://jsoup.org/cookbook/extracting-data/selector-syntax

Upvotes: 4

Related Questions