![](/img/trans.png)
[英]Extract only HTML tags and attributes from a html string using Jsoup
[英]Set attributes of tags of HTML using JSoup
如何使用JSoup设置HTML标签的属性?
我想使用Jsoup Library在Java中设置标签 - >“img”的属性 - >“src”。
Elements img_attributes = doc.select("img[src^=/im]");
for(Element img_attribute: img_attributes)
{
String s = img_attribute.attr("src");
System.out.println(s);
}
此代码打印src
值。 我想更改src
值。
您可以通过两种方式使用attr()
方法执行此操作:循环或直接在Elements
对象上:
// In a loop
for( Element img : doc.select("img[src]") )
{
img.attr("src", "your-source-here"); // set attribute 'src' to 'your-source-here'
}
// Or directly on the 'Elements'
doc.select("img[src]").attr("src", "your-value-here");
实际上两种解决方案都是相同的。
检查http://jsoup.org/apidocs/org/jsoup/nodes/Element.html#attr%28java.lang.String,%20java.lang.String%29我认为功能
public Element attr(String attributeKey,
String attributeValue)
对你有用
请修改它并通过doc将其写入您的html文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.