Kotlin

Kotlin Jsoup 爬蟲 圖片下載到本地

1.導入Jsoup

dependencies {
    implementation 'org.jsoup:jsoup:1.11.2'
}

2.創建HTMLParser

import org.jsoup.Jsoup
import org.jsoup.nodes.Element
import org.jsoup.select.Elements

object HTMLParser {
    fun getElementsByAttributeValue(html: String, attribute: String, name: String): Elements {
        val document = Jsoup.parse(html)
        return document.html(html).getElementsByAttributeValue(attribute, name)
    }

    fun getElementsByClass(html: String, className: String, name: String): Elements {
        val document = Jsoup.parse(html)
        return document.html(html).getElementsByClass(className)
    }

    fun getElementsByTag(html: String, tagName: String): Elements {
        val document = Jsoup.parse(html)
        return document.html(html).getElementsByTag(tagName)
    }

    fun getElementsById(html: String, id: String): Element {
        val document = Jsoup.parse(html)
        return document.html(html).getElementById(id)
    }
}

3.取得爬蟲資料並下載圖片

import java.io.File
import java.net.URL

fun main() {
    //創建girl資料夾到D槽
    val file = File("D:\\girl")
    if (!file.exists()) {
        file.mkdirs()
    }

    val url = URL("http://www.sdxsl.com/gaoqingtaotu/")
    val html = url.readText()
    val elements = HTMLParser.getElementsByTag(html, "img")

    for (element in elements) {
        //取出圖片路徑
        val src = element.attr("src")
        //取出圖片名稱
        val name = element.attr("alt")

        val file1 = File("d:\\girl\\$name.jpg")
        val openConnection = URL(src).openConnection()
        val bytes = openConnection.getInputStream().readBytes()
        file1.writeBytes(bytes)
    }
    //圖片儲存完成
    print("OK")
}

4.效果展示

發表迴響