简体   繁体   English

Java使用JSoup从html获取字符串列表

[英]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. 我需要从URL获取名称为“keywords”的元标记的内容

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

How can i do this with JSoup ? 我怎么能用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: 当谈到没有ID的元素时,我不知道我在做什么,给出的错误非常简单:

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)

change 更改

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

to

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM