Java retrieves a list of strings from html using JSoup

I need to get meta tag content called "keywords" from a URL.

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

How can I do this with JSoup ?

I tried to get the element by class, and then tried to get the content if that name was a keyword but no luck:

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

I do not know what I am doing when it comes to an element without an identifier, the error indicated is pretty simple:

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)
+4
source share
1 answer

change

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

to

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

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

+4
source

Source: https://habr.com/ru/post/1547846/


All Articles