我有以下代码,应该从 HTML 文档中提取数据。我用的是 eclipse 。它给了我两个错误(不过,此代码是从 JSoup 站点作为教程复制并粘贴的)。 1) 文件和 2) 元素中的错误。我看不出这两种类型有什么问题。
导入java.io.IOException; 导入 java.net.MalformedURLException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
public class TestClass
{
public static void main(String args[]) throws IOException
{
try{
File input = new File("/tmp/input.html");
Document doc = Jsoup.parse(input, "UTF-8", "http://example.com/");
Element content = doc.getElementById("content");
Elements links = content.getElementsByTag("a");
for (Element link : links) {
String linkHref = link.attr("href");
String linkText = link.text();
}
}//try
catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}//catch
}
}</i>
最佳答案
您忘记导入
它们。
import java.io.File;
import org.jsoup.select.Elements;
另请参阅:
<小时/>提示:阅读 Eclipse 建议的“快速修复”选项。它已经是File
的第一个选项。
关于java - JSoup 数据类型错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11144858/