![](/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.