![](/img/trans.png)
[英]How to extract text from all the elements in a webpage individually, using JSoup?
[英]How to extract main text from website and exclude archive and link to other webpage using JSOUP
我只知道如何提取主要文本并排除注释,但无法排除存档并链接到其他网页。
这是我的代码:
package CrawlerMain;
import java.io.IOException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Node;
public class MainFour {
public static void main(String[] args) throws IOException {
Document doc = Jsoup.connect("http://www.papagomo.com").get();
//get text only
removeComments(doc);
String text = doc.body().text();
System.out.println(text);
}
private static void removeComments(Node node) {
int i = 0;
while (i < node.childNodes().size()) {
Node child = node.childNode(i);
if (child.nodeName().equals("#comment"))
child.remove();
else {
removeComments(child);
i++;
}
} //To change body of generated methods, choose Tools | Templates.
}
}
这是一个例子,但还没有完成。 你必须添加一些过滤来删除你不想要的一切:
Document doc = Jsoup.connect("http://www.papagomo.com").get();
for( Element element : doc.select("div") ) // Select only 'div' tags
{
final String ownText = element.ownText(); // Own text of this element
if( ownText.isEmpty() )
{
continue; // Skip empty tags
}
else
{
System.out.println(ownText); // Output to see the result
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.