使用 HTTPClient 获取网页文本

发布于 2024-09-08 21:19:14 字数 409 浏览 6 评论 0原文

我刚刚开始使用 HTTPClient,我想获取一个网页并从中提取原始文本减去所有 html 标记。

HTTPClient 可以做到这一点吗?如果是这样,怎么办?或者我应该看看另一个图书馆吗?

例如,如果页面包含

<body><p>para1 test info</p><div><p>more stuff here</p></div>

我希望它输出

para1 test info more stuff here

I'm just getting started with HTTPClient, and I want to take a webpage and extract out the raw text from it minus all the html markup.

Can HTTPClient accomplish that? If so, how? Or is there another library I should be looking at?

for example if the page contains

<body><p>para1 test info</p><div><p>more stuff here</p></div>

I'd like it to output

para1 test info more stuff here

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

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

发布评论

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

评论(4

乄_柒ぐ汐 2024-09-15 21:19:15

不会。HttpClient 处理网络协议 - 发送请求和接收响应。收到回复后,您需要决定如何处理该回复。也就是说,您可以按照其他人的建议使用其他库来解析 HTML。

No. HttpClient handles network protocol - sending requests and receiving responses. It's up to you to figure out what to do with the response once you receive it. That said, you can use other libraries to parse HTML as others suggested.

苦行僧 2024-09-15 21:19:15

HTML 解析器库 可能就是您正在寻找的。它允许从 HTML 文档中提取内容。

The HTML Parser library might be what you are looking for. It allows for extraction of content from a HTML document.

美煞众生 2024-09-15 21:19:15

正如其他人提到的,您需要一个 HTML 解析库。 这里是一个相关的问题。

As others have mentioned, you need an HTML parsing library. Here is a relevant question.

2024-09-15 21:19:14

我建议使用 HttpComponents Client (HTTPClient 4) (而不是版本 3您已链接到)。

也就是说,它独立于 HTTP 客户端库(还有其他库)。您需要的是将 HTML 转换为纯文本。这可能很有趣: http://www.rgagnon.com/javadetails/java- 0424.html

I'd suggest using HttpComponents Client (HTTPClient 4) (instead of version 3 you've linked to).

This being said, it's independent of the HTTP client library (there are others). What you need is to convert the HTML into plain text. This could be of interest: http://www.rgagnon.com/javadetails/java-0424.html

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