jsoup doc.select 获取所有格式图片 方法
时间: 2024-05-29 21:13:02 浏览: 9
可以使用以下代码获取所有格式的图片:
Elements imgElements = doc.select("img");
for (Element img : imgElements) {
String imgUrl = img.attr("src");
System.out.println(imgUrl);
}
相关问题
import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.nodes.Entities; import org.jsoup.parser.Parser; import org.jsoup.safety.Whitelist; import org.jsoup.select.Elements; public class HtmlFormatter { public static void main(String[] args) { String html = "<html><body><h1>Hello, World!</h1><p>This is a paragraph.</p></body></html>"; // 解析HTML文档 Document doc = Jsoup.parse(html, "", Parser.xmlParser()); // 设置缩进和换行 doc.outputSettings().indentAmount(4).prettyPrint(true); // 格式化HTML String formattedHtml = doc.html(); System.out.println(formattedHtml); } }转成kotlin代码
以下是将给定的Java代码转换为Kotlin代码的示例:
```kotlin
import org.jsoup.Jsoup
import org.jsoup.nodes.Document
import org.jsoup.parser.Parser
fun main() {
val html = "<html><body><h1>Hello, World!</h1><p>This is a paragraph.</p></body></html>"
// 解析HTML文档
val doc: Document = Jsoup.parse(html, "", Parser.xmlParser())
// 设置缩进和换行
doc.outputSettings().indentAmount(4).prettyPrint(true)
// 格式化HTML
val formattedHtml: String = doc.html()
println(formattedHtml)
}
```
这段Kotlin代码与之前的Java代码几乎相同,只需进行以下更改:
1. 删除导入语句中的无用类(Element、Entities、Whitelist等)。
2. 将Java的main方法转换为Kotlin的main函数,不再需要参数args。
3. 删除类型声明,Kotlin可以自动推断变量类型。
4. Kotlin中字符串使用双引号表示。
希望这可以帮助到你!如果你有任何其他问题,请随时提问。
foreach not applicable to type 'org.jsoup.select.Elements'
如果你在使用 `for-each` 循环来遍历 `Elements` 对象时遇到了 `foreach not applicable to type 'org.jsoup.select.Elements'` 错误,那么可能是因为 `Elements` 对象不支持 `for-each` 循环。解决方法是将 `Elements` 对象转换为 `List<Element>` 类型,然后再使用 `for-each` 循环遍历。以下是一个示例代码:
```java
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
public class HtmlParser {
public static void main(String[] args) {
String html = "<html><body><p>This is a paragraph</p><table><tr><td>Table cell</td></tr></table></body></html>";
Document doc = Jsoup.parse(html);
Element body = doc.body();
// 获取所有文本节点
Elements textNodes = body.select(":not(table) > *").not("table *").not("table");
// 将Elements对象转换为List类型
List<Element> textNodeList = textNodes.stream().collect(Collectors.toList());
// 遍历List
for (Element textNode : textNodeList) {
System.out.println(textNode.text());
}
}
}
```
在上述代码中,我们将 `Elements` 对象通过 `stream()` 方法转换为 `Stream<Element>` 对象,然后使用 `collect(Collectors.toList())` 方法将 `Stream<Element>` 对象转换为 `List<Element>` 对象。最后,我们使用 `for-each` 循环遍历 `List<Element>` 对象。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)