简单的问题,这是 Jsoup introduction page 中的示例:
String html = "<html><head><title>First parse</title></head>"
+ "<body><p>Parsed HTML into a doc.</p></body></html>";
Document doc = Jsoup.parse(html);
现在这对我不起作用,Netbeans 说您不能只从节点文档转到文档类型。很公平,所以我输入后错误就消失了。就像这样:
编辑:这不起作用!
String html = "<html><head><title>First parse</title></head>"
+ "<body><p>Parsed HTML into a doc.</p></body></html>";
Document doc = (Document) Jsoup.parse(html);
TYPECAST 不起作用:
Exception in thread "main" java.lang.ClassCastException: org.jsoup.nodes.Document cannot be cast to org.w3c.dom.Document
at scraping.Scraping.main(Scraping.java:24)
(当学习新东西时,你会从格式与文档所示完全相同的示例中得到所有这些错误,这有点令人沮丧)
编辑:这是错误:
required: org.w3c.dom.Document
found: ord.jsoup.nodes.Document
非常感谢
最佳答案
如果有人感兴趣,文档类型是 Jsoup 库的一部分,可以通过导入访问:
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
这使得代码可以正常工作(请参阅 Jsoup 网页上的示例)
关于java - 我必须进行类型转换,但他们的示例不需要(Jsoup 文档类型),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15986818/