Jsoup Java For 循环和元素
Jsoup Java For Loops and Elements
我正在学习用于 java 的 jsoup。首先,我不太了解 jsoup "Elements" 和 jsoup "Element" 之间的区别以及何时使用它们。这是我正在尝试做的一个例子。使用这个 url http://en.wikipedia.org/wiki/List_of_bow_tie_wearers#Architects 我想解析类别 "Architects" 下的文本名称。我试过这个:
Document doc = null;
try {
doc = Jsoup.connect("http://en.wikipedia.org/wiki/List_of_bow_tie_wearers").get();
} catch (IOException e) {
}
Elements basics = doc.getElementsByClass("mw-redirect");
String text = basics.text();
System.out.println(text);
}
这是输出:
run:
Franklin Roosevelt Arthur Schlesinger, Jr. Reagan administration University of Colorado at Boulder Eric R. Kandel Eugene H. Spafford Arthur Schlesinger, Jr. middle finger John Daly Sir Robin Day Today show Tom Oliphant Today show Harry Smith TV chef Panic! At The Disco Watergate Watergate Hillary Clinton Donald M. Payne, Jr. Franklin Roosevelt Baldwin–Wallace College Howard Phillips Twilight Sparkle Gil Chesterton Bertram Cooper Richard Gilmore Dr. Donald "Ducky" Mallard, M.D., M.E. Medical Examiner Brother Mouzone hitman Buckaroo Banzai Conan Edogawa Jack Point Waylon Smithers Franklin Roosevelt NFL Chronicle of Higher Education Evening Standard
我真的只是想学习遍历 HTML 文档的基础知识,但我在使用 jsoup 说明书时遇到了问题,因为它让初学者感到困惑。感谢任何帮助。
关于你的第一个问题,Elements和Element的区别,顾名思义就是项目的数量。
元素类型的对象包含一个 HTML 节点。类型元素之一。
如果您查看 Element and Elements 的 api 文档中的构造函数,它会变得相当明显。
现在是解析部分。在您的代码中,您正在寻找 "mw-redirect",这是不够的。您需要先 "navigate" 到正确的部分。
我在这里做了一个工作示例:
Document doc = null;
try {
doc = Jsoup.connect("http://en.wikipedia.org/wiki/List_of_bow_tie_wearers").get();
} catch (IOException e) {
}
if (doc != null) {
// The Architect headline has an id. Awesome! Let's select it.
Element architectsHeadline = doc.select("#Architects").first();
// For educational purposes, let's see what we've got there...
System.out.println(architectsHeadline.html());
// Now, we use some other selector then .first(), since we need to
// get the tag after the h3 with id Architects.
// We jump back to the h3 using .parent() and select the succeding tag
Element architectsList = architectsHeadline.parent().nextElementSibling();
// Again, let's have a peek
System.out.println(architectsList.html());
// Ok, now since we have our list, let's traverse it.
// For this we select every a tag inside each li tag
// via a css selector
for(Element e : architectsList.select("li > a")){
System.out.println(e.text());
}
}
希望对您有所帮助。
我正在学习用于 java 的 jsoup。首先,我不太了解 jsoup "Elements" 和 jsoup "Element" 之间的区别以及何时使用它们。这是我正在尝试做的一个例子。使用这个 url http://en.wikipedia.org/wiki/List_of_bow_tie_wearers#Architects 我想解析类别 "Architects" 下的文本名称。我试过这个:
Document doc = null;
try {
doc = Jsoup.connect("http://en.wikipedia.org/wiki/List_of_bow_tie_wearers").get();
} catch (IOException e) {
}
Elements basics = doc.getElementsByClass("mw-redirect");
String text = basics.text();
System.out.println(text);
}
这是输出:
run:
Franklin Roosevelt Arthur Schlesinger, Jr. Reagan administration University of Colorado at Boulder Eric R. Kandel Eugene H. Spafford Arthur Schlesinger, Jr. middle finger John Daly Sir Robin Day Today show Tom Oliphant Today show Harry Smith TV chef Panic! At The Disco Watergate Watergate Hillary Clinton Donald M. Payne, Jr. Franklin Roosevelt Baldwin–Wallace College Howard Phillips Twilight Sparkle Gil Chesterton Bertram Cooper Richard Gilmore Dr. Donald "Ducky" Mallard, M.D., M.E. Medical Examiner Brother Mouzone hitman Buckaroo Banzai Conan Edogawa Jack Point Waylon Smithers Franklin Roosevelt NFL Chronicle of Higher Education Evening Standard
我真的只是想学习遍历 HTML 文档的基础知识,但我在使用 jsoup 说明书时遇到了问题,因为它让初学者感到困惑。感谢任何帮助。
关于你的第一个问题,Elements和Element的区别,顾名思义就是项目的数量。
元素类型的对象包含一个 HTML 节点。类型元素之一。
如果您查看 Element and Elements 的 api 文档中的构造函数,它会变得相当明显。
现在是解析部分。在您的代码中,您正在寻找 "mw-redirect",这是不够的。您需要先 "navigate" 到正确的部分。
我在这里做了一个工作示例:
Document doc = null;
try {
doc = Jsoup.connect("http://en.wikipedia.org/wiki/List_of_bow_tie_wearers").get();
} catch (IOException e) {
}
if (doc != null) {
// The Architect headline has an id. Awesome! Let's select it.
Element architectsHeadline = doc.select("#Architects").first();
// For educational purposes, let's see what we've got there...
System.out.println(architectsHeadline.html());
// Now, we use some other selector then .first(), since we need to
// get the tag after the h3 with id Architects.
// We jump back to the h3 using .parent() and select the succeding tag
Element architectsList = architectsHeadline.parent().nextElementSibling();
// Again, let's have a peek
System.out.println(architectsList.html());
// Ok, now since we have our list, let's traverse it.
// For this we select every a tag inside each li tag
// via a css selector
for(Element e : architectsList.select("li > a")){
System.out.println(e.text());
}
}
希望对您有所帮助。