我看看那个页面的源代码 - 这是绊倒了解析的是,所有真正的内容被包裹在评论的东西,像这样:
<code class="hidden_elem" id="u_0_42"><!-- <div class="fbTimelineSection ...> --></code>
有JS上将数据提升到真实DOM的页面,但由于jsoup不执行JS,所以它保留为注释。因此,在提取内容之前,我们需要模拟JS并“隐藏”这些元素。以下是一个示例,帮助您开始:
String url = "https://www.facebook.com/cedarstreettimes?fref=ts";
String ua = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/537.33 (KHTML, like Gecko) Chrome/27.0.1438.7 Safari/537.33";
Document doc = Jsoup.connect(url).userAgent(ua).timeout(10*1000).get();
// move the hidden commented out html into the DOM proper:
Elements hiddenElements = doc.select("code.hidden_elem");
for (Element hidden: hiddenElements) {
for (Node child: hidden.childNodesCopy()) {
if (child instanceof Comment) {
hidden.append(((Comment) child).getData()); // comment data parsed as html
}
}
}
Elements articles = doc.select("div[role=article]");
for (Element article: articles) {
if (article.select("span.userContent").size() > 0) {
String text = article.select("span.userContent").text();
String imgUrl = article.select("div.photo img").attr("abs:src");
System.out.println(String.format("%s\n%s\n\n", text,imgUrl));
}
}
该示例提取了文章文本以及与其关联的任何照片。
(这可能是最好使用FB API,这种方法。我想告诉你如何效仿JS的小位做出刮正常工作)
是否有可能获取文本评论评论过交? – Prasad 2013-09-24 14:30:38
是否有可能通过使用jsoup 例如 从facebook页面获得newlikes计数https://www.facebook.com/blick/likes 从页面我需要新页面赞成计数。 – 2015-06-30 09:53:11