使用Jsoup提取句子的屏幕刮痧

时间:2013-12-30 02:47:07

标签: java jsoup screen-scraping

我想做一些屏幕抓取,经过一些研究后,似乎JSoup是完成此任务的最佳工具。我希望能够在网页上提取所有句子;例如,给定这个维基百科页面http://en.wikipedia.org/wiki/Data_scraping#Screen_scraping,我希望能够获得该页面上的所有句子并将其打印到控制台。我仍然不熟悉JSoup如何工作,所以如果有人能帮助我,那将非常感激。谢谢!

1 个答案:

答案 0 :(得分:2)

首先下载Jsoup并将其包含在您的项目中。然后,最好的起点是Jsoup食谱(http://jsoup.org/cookbook/),因为它提供了将与Jsoup一起使用的最常用方法的示例。我建议您花一些时间来完成这些示例以熟悉API。另一个好的资源是javadocs

以下是从您提供的Wikipedia链接中提取一些文字的快速示例:

String url = "http://en.wikipedia.org/wiki/Data_scraping#Screen_scraping";  

// Download the HTML and store in a Document
Document doc = Jsoup.connect(url).get();

// Select the <p> Elements from the document    
Elements paragraphs = doc.select("p");

// For each selected <p> element, print out its text
for (Element e : paragraphs) {
    System.out.println(e.text());
}