如何使用 jsoup 替换标签
我想用 div
标签替换所有图像标签。我可以选择所有标签,并且我知道我必须使用 replaceWith
。但我无法使用它。
如果我使用 TextNode
将其替换为
并转换为 ;<div>我的 div &lt;/div>
我知道 <
和 >
是用于<
和 >
请帮助我。
I want to replace all image tags with div
tag. I am able to select all tags and I know that I have to use replaceWith
. But I am unable to use it.
And if I use TextNode
for replacing it with <div> </div>
and it converts into <div> my div </div>
I know <
and >
is for <
and >
Please help me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我猜您正在替换为
element.replaceWith(new TextNode("
"),"");
?Textnode
用于文本和转义内容 - 这就是您看到 HTML 实体的原因。您需要替换为标签,因此请执行诸如element.replaceWith(new Element(Tag.valueOf("div"), ""));
之类的操作。I guess you are replacing with
element.replaceWith(new TextNode("<div></div>"),"");
?A
Textnode
is for text and escapes content - thats why you see the HTML entities. You need to replace with a tag, so do something likeelement.replaceWith(new Element(Tag.valueOf("div"), ""));
.这是实现这一目标的最简单方法:
希望这对您有用。
This is the simplest way of achieving this:
Hope that is working for you.
只需使用 find 方法并传递特定标签
let tag = soup.find('tag_name') tag.name = 'new_tag';//替换标签
对于多个标签,只需放入循环内
let tag = soup.findAll('tag_name'); if (tag) { for (let i = 0; i < tag.length; i++) { tag[i].name = 'your_new_tag'; } } }
just use find method and pass specific tags
let tag = soup.find('tag_name') tag.name = 'new_tag';//replaced tag
for multiple tag just put inside loop
let tag = soup.findAll('tag_name'); if (tag) { for (let i = 0; i < tag.length; i++) { tag[i].name = 'your_new_tag'; } }