小编典典
使用类似Jsoup的HTML解析器。我优先于Java中的其他HTML解析器,因为它像CSS选择器一样支持 jQuery。此外,它的代表节点列表类,工具,让你可以在遍历它增强的for循环(所以没有必要的麻烦与冗长而像一般的Java DOM解析器类)。Elements Iterable Node NodeList
这是一个基本的启动示例(只需将最新的Jsoup JAR文件放入类路径中):
package com.stackoverflow.q2835505;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
public class Test {
public static void main(String[] args) throws Exception {
String url = "https://stackoverflow.com/questions/2835505";
Document document = Jsoup.connect(url).get();
String question = document.select("#question .post-text").text();
System.out.println("Question: " + question);
Elements answerers = document.select("#answers .user-details a");
for (Element answerer : answerers) {
System.out.println("Answerer: " + answerer.text());
}
}
}
你可能已经猜到了,这会打印出你自己的问题以及所有答题者的姓名。
2020-03-09