我看看那個頁面的源代碼 - 這是絆倒了解析的是,所有真正的內容被包裹在評論的東西,像這樣:
<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