Jsoup:如何提取文件名中带有 space 的 img?
Jsoup : how to extract img with space in filename?
我正在尝试使用 Jsoup 提取 img。它适用于文件名中没有任何 space 的图像,但如果有白色 space.
它只提取第一部分
我试过下面的方法。
String result = Jsoup.clean(content,"https://rally1.rallydev.com/", Whitelist.relaxed().preserveRelativeLinks(true), new Document.OutputSettings().prettyPrint(false));
Document doc = Jsoup.parse(result);
Elements images = doc.select("img");
例如HTML内容
Description:<div>some text content<br /></div>
<div><img src=/slm/attachment/43647556403/My file with space.png /></div>
<div><img src=/slm/attachment/43648152373/my_file_without_space.png/></div>
result
内容为:
Description:Some text content<br> <img src="/slm/attachment/43647556403/My"><img src="/slm/attachment/43648152373/my_file_without_space.png/">
in "result" 对于文件名中带有 space 的图像只有第一部分 "My"。忽略whitespace.
后面的内容
如果文件名包含 space,如何提取文件名?
你可以这样:
Elements images = doc.select("img");
for(Element image: images){
String imgSrc = image.attr("src");
imgSrc = imgSrc.subString(imgSrc.lastIndexOf("/"), imgSrc.length()); // this will give you name.png
}
这个问题在Jsoup中不容易解决,因为带空格的例子的src
属性值实际上被正确识别为只有My
。 file
、with
和 space.png
部分在此示例中也是没有值的属性。当然,您可以使用 JSoup 将 src 属性后面的属性键连接到它的值。例如像这样:
String test =""
+ "<div><img src=/slm/attachment/43647556403/My file with space.png /></div>"
+ "<div><img src=/slm/attachment/43647556403/My file with space.png name=whatever/></div>"
+ "<div><img src=/slm/attachment/43647556403/This breaks it.png name=whatever/></div>"
+ "<div><img src=\"/slm/attachment/43647556403/This works.png\" name=whatever/></div>"
+ "<div><img src=/slm/attachment/43648152373/my_file_without_space.png/></div>";
Document doc = Jsoup.parse(test);
Elements imgs = doc.select("img");
for (Element img : imgs){
Attribute src = null;
StringBuffer newSrcVal = new StringBuffer();
List<String> toRemove = new ArrayList<>();
for (Attribute a : img.attributes()){
if (a.getKey().equals("src")){
newSrcVal.append(a.getValue());
src = a;
}
else if (newSrcVal.length()>0){
//we already found the scr tag
if (a.getValue().isEmpty()){
newSrcVal.append(" ").append(a.getKey());
toRemove.add(a.getKey());
}
else{
//the empty attributes, i.e. file name parts are over
break;
}
}
}
for (String toRemAttr : toRemove){
img.removeAttr(toRemAttr);
}
src.setValue(newSrcVal.toString());
}
System.out.println(doc);
此算法循环遍历所有 img 元素,并在每个 img 中循环遍历其属性。当它找到 src
属性时,它会保留它以供参考并开始填充 newSrcBuf
StringBuffer。所有以下无值属性将被添加到 newSrcBuf
,直到找到另一个具有值的属性或没有更多属性。最后,用 newSrcBuf
的内容重置 scr 属性值,并从 DOM.
中删除以前的空属性
请注意,当您的文件名包含两个或多个连续空格时,这将不起作用。 JSoup 会丢弃属性之间的那些空格,因此您无法在解析后取回它们。如果需要,则需要在解析之前操作输入 html。
我正在尝试使用 Jsoup 提取 img。它适用于文件名中没有任何 space 的图像,但如果有白色 space.
它只提取第一部分我试过下面的方法。
String result = Jsoup.clean(content,"https://rally1.rallydev.com/", Whitelist.relaxed().preserveRelativeLinks(true), new Document.OutputSettings().prettyPrint(false));
Document doc = Jsoup.parse(result);
Elements images = doc.select("img");
例如HTML内容
Description:<div>some text content<br /></div>
<div><img src=/slm/attachment/43647556403/My file with space.png /></div>
<div><img src=/slm/attachment/43648152373/my_file_without_space.png/></div>
result
内容为:
Description:Some text content<br> <img src="/slm/attachment/43647556403/My"><img src="/slm/attachment/43648152373/my_file_without_space.png/">
in "result" 对于文件名中带有 space 的图像只有第一部分 "My"。忽略whitespace.
后面的内容如果文件名包含 space,如何提取文件名?
你可以这样:
Elements images = doc.select("img");
for(Element image: images){
String imgSrc = image.attr("src");
imgSrc = imgSrc.subString(imgSrc.lastIndexOf("/"), imgSrc.length()); // this will give you name.png
}
这个问题在Jsoup中不容易解决,因为带空格的例子的src
属性值实际上被正确识别为只有My
。 file
、with
和 space.png
部分在此示例中也是没有值的属性。当然,您可以使用 JSoup 将 src 属性后面的属性键连接到它的值。例如像这样:
String test =""
+ "<div><img src=/slm/attachment/43647556403/My file with space.png /></div>"
+ "<div><img src=/slm/attachment/43647556403/My file with space.png name=whatever/></div>"
+ "<div><img src=/slm/attachment/43647556403/This breaks it.png name=whatever/></div>"
+ "<div><img src=\"/slm/attachment/43647556403/This works.png\" name=whatever/></div>"
+ "<div><img src=/slm/attachment/43648152373/my_file_without_space.png/></div>";
Document doc = Jsoup.parse(test);
Elements imgs = doc.select("img");
for (Element img : imgs){
Attribute src = null;
StringBuffer newSrcVal = new StringBuffer();
List<String> toRemove = new ArrayList<>();
for (Attribute a : img.attributes()){
if (a.getKey().equals("src")){
newSrcVal.append(a.getValue());
src = a;
}
else if (newSrcVal.length()>0){
//we already found the scr tag
if (a.getValue().isEmpty()){
newSrcVal.append(" ").append(a.getKey());
toRemove.add(a.getKey());
}
else{
//the empty attributes, i.e. file name parts are over
break;
}
}
}
for (String toRemAttr : toRemove){
img.removeAttr(toRemAttr);
}
src.setValue(newSrcVal.toString());
}
System.out.println(doc);
此算法循环遍历所有 img 元素,并在每个 img 中循环遍历其属性。当它找到 src
属性时,它会保留它以供参考并开始填充 newSrcBuf
StringBuffer。所有以下无值属性将被添加到 newSrcBuf
,直到找到另一个具有值的属性或没有更多属性。最后,用 newSrcBuf
的内容重置 scr 属性值,并从 DOM.
请注意,当您的文件名包含两个或多个连续空格时,这将不起作用。 JSoup 会丢弃属性之间的那些空格,因此您无法在解析后取回它们。如果需要,则需要在解析之前操作输入 html。