使用 HTTPClient 获取网页文本
我刚刚开始使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不会。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.
HTML 解析器库 可能就是您正在寻找的。它允许从 HTML 文档中提取内容。
The HTML Parser library might be what you are looking for. It allows for extraction of content from a HTML document.
正如其他人提到的,您需要一个 HTML 解析库。 这里是一个相关的问题。
As others have mentioned, you need an HTML parsing library. Here is a relevant question.
我建议使用 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