我如何使用与Apache提卡HTML解析器在Java中提取所有的HTML标签? [英] How can I use the HTML parser with Apache Tika in Java to extract all HTML tags?

查看:252
本文介绍了我如何使用与Apache提卡HTML解析器在Java中提取所有的HTML标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我下载蒂卡核心和蒂卡的解析器库,但我找不到例如codeS到HTML文档解析字符串?我必须摆脱一个网页的源代码的所有HTML标签。我能做什么?我如何$是使用Apache提卡?

I download tika-core and tika-parser libraries, but I could not find the example codes to parse HTML documents to string? I have to get rid of all html tag of source of a web page. What can I do? How do I code that using Apache Tika?

推荐答案

你想一个HTML文件的纯文本版本?如果是这样,你需要的是这样的:

Do you want a plain text version of a html file? If so, all you need is something like:

        InputStream input = new FileInputStream("myfile.html");
        ContentHandler handler = new BodyContentHandler();
        Metadata metadata = new Metadata();
        new HtmlParser().parse(input, handler, metadata, new ParseContext());
        String plainText = handler.toString();

该BodyContentHandler,当不带参数的构造函数或用字符限制创建的,将捕获的html正文的文本(只),并将其返还给您。

The BodyContentHandler, when created with no constructor arguments or with a character limit, will capture the text (only) of the body of the html and return it to you.

这篇关于我如何使用与Apache提卡HTML解析器在Java中提取所有的HTML标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆