解决方法
我会推荐
Jsoup.它有一个非常好的API,支持
jQuery like CSS selectors and non-verbose element iteration.以
this answer的副本为例,这里打印出你自己的问题和所有回答者的名字:
URL url = new URL("https://stackoverflow.com/questions/3121136"); Document document = Jsoup.parse(url,3000); 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()); }
另一种选择是XPath,但JSoup对于已经很好地掌握CSS选择器的web开发人员更有用.