`
小网客
  • 浏览: 1216447 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

基于boilerpipe抽取页面乱码问题解决方式

 
阅读更多

需求:

基于boilerpipe抽取页面的文本内容,基于url的openStream来获取页面的时候会碰到乱码,解决方式是基于jsoup来获取body的byte流

实现:

jar依赖:

<dependency>
	<groupId>com.syncthemall</groupId>
	<artifactId>boilerpipe</artifactId>
	<version>1.2.2</version>
</dependency>

抽取实现:

private String extractContent(String url) throws Exception {
	InputStream inputStream = new ByteArrayInputStream(getEmptyConnection(
			url).execute().bodyAsBytes());

	TextDocument doc = new BoilerpipeSAXInput(new InputSource(inputStream))
			.getTextDocument();

	BoilerpipeExtractor extractor = CommonExtractors.DEFAULT_EXTRACTOR;
	extractor.process(doc);
	return doc.getContent();
}

 

0
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics