我正在尝试解析以下 URL 的 html:
http://ocw.mit.edu/courses/aeronautics-and-astronautics/16-050-thermal-energy-fall-2002/
获取包含教师姓名的“
”标签的文本。所需信息位于“< p >”标签内,但我无法使用 JSoup 检索标签。我不知道我做错了什么,因为当我将标签保存在 Element 对象中时,我们称它为“b”,我调用 b.getAllElements() 它不显示
作为元素之一.这不是 Jsoup 的 getAllElements() 方法所做的吗?如果没有,请有人向我解释我显然缺少的层次结构,因为解析器无法找到包含我需要的文本的
标签,在这种情况下是“Zoltan Spakovszky 教授” .
如有任何帮助,我们将不胜感激。
public void getHomePageLinks()
{
String html = "http://ocw.mit.edu/courses/aeronautics-and-astronautics/16-050-thermal-energy-fall-2002/";
org.jsoup.nodes.Document doc = Jsoup.parse(html);
Elements bodies = doc.select("body");
for(Element body : bodies )
{
System.out.println(body.getAllElements());
}
}
输出是:
http://ocw.mit.edu/courses/aeronautics-and-astronautics/16-050-thermal-energy-fall-2002/
不是应该打印出文档中body标签内的所有元素吗?
最佳答案
我对 JSoup 一无所知,但似乎如果你想要讲师的名字,你可以通过类似的方式访问它:
Element instructor = doc.select("div.chpstaff div p");
关于java - 使用 JSoup 解析 HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12361925/