Java中如何从HTML文件中获取值?

发布于 2024-10-10 07:11:25 字数 221 浏览 0 评论 0原文

我需要从 HTML 文件中获取一个值(下面示例中的“abc”),如下所示:

          <input type="hidden" name="something" value="abc" />

正如我从其他帖子中发现的那样,我应该使用 HTML 解析器之一(而不是正则表达式)。您能告诉我使用哪一个或显示代码示例吗?

谢谢。

I need to get a value ("abc" in below example) from HTML file that looks like this:

          <input type="hidden" name="something" value="abc" />

As i found out from other posts, i should be using one of the HTML parsers (not regex). Could you please tell me which one to use or show a code sample.

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

千寻… 2024-10-17 07:11:25

您可以使用 Jsoup 来实现此目的。

File file = new File("/path/to/file.html");
Document document = Jsoup.parse(file, "UTF-8");
Element something = document.select("input[name=something]").first();
String value = something.val();
System.out.println(value); // abc
// ...

或者更短:

String value = Jsoup.parse(new File("/path/to/file.html"), "UTF-8").select("input[name=something]").first().val();
System.out.println(value); // abc
// ...

另请参阅:

You could use Jsoup for this.

File file = new File("/path/to/file.html");
Document document = Jsoup.parse(file, "UTF-8");
Element something = document.select("input[name=something]").first();
String value = something.val();
System.out.println(value); // abc
// ...

Or shorter:

String value = Jsoup.parse(new File("/path/to/file.html"), "UTF-8").select("input[name=something]").first().val();
System.out.println(value); // abc
// ...

See also:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文