{"id":1110,"date":"2021-01-28T13:34:26","date_gmt":"2021-01-28T05:34:26","guid":{"rendered":"https:\/\/badgameshow.com\/fly\/?p=1110"},"modified":"2021-01-28T13:36:02","modified_gmt":"2021-01-28T05:36:02","slug":"kotlin-jsoup-%e7%88%ac%e8%9f%b2-%e5%9c%96%e7%89%87%e4%b8%8b%e8%bc%89%e5%88%b0%e6%9c%ac%e5%9c%b0","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/fly\/kotlin-jsoup-%e7%88%ac%e8%9f%b2-%e5%9c%96%e7%89%87%e4%b8%8b%e8%bc%89%e5%88%b0%e6%9c%ac%e5%9c%b0\/","title":{"rendered":"Kotlin Jsoup \u722c\u87f2 \u5716\u7247\u4e0b\u8f09\u5230\u672c\u5730"},"content":{"rendered":"<h3>1.\u5c0e\u5165Jsoup<\/h3>\n<pre><code class=\"language-Gradle line-numbers\">dependencies {\n    implementation 'org.jsoup:jsoup:1.11.2'\n}\n<\/code><\/pre>\n<h3>2.\u5275\u5efaHTMLParser<\/h3>\n<pre><code class=\"language-Kotlin line-numbers\">import org.jsoup.Jsoup\nimport org.jsoup.nodes.Element\nimport org.jsoup.select.Elements\n\nobject HTMLParser {\n    fun getElementsByAttributeValue(html: String, attribute: String, name: String): Elements {\n        val document = Jsoup.parse(html)\n        return document.html(html).getElementsByAttributeValue(attribute, name)\n    }\n\n    fun getElementsByClass(html: String, className: String, name: String): Elements {\n        val document = Jsoup.parse(html)\n        return document.html(html).getElementsByClass(className)\n    }\n\n    fun getElementsByTag(html: String, tagName: String): Elements {\n        val document = Jsoup.parse(html)\n        return document.html(html).getElementsByTag(tagName)\n    }\n\n    fun getElementsById(html: String, id: String): Element {\n        val document = Jsoup.parse(html)\n        return document.html(html).getElementById(id)\n    }\n}\n<\/code><\/pre>\n<h3>3.\u53d6\u5f97\u722c\u87f2\u8cc7\u6599\u4e26\u4e0b\u8f09\u5716\u7247<\/h3>\n<pre><code class=\"language-Kotlin line-numbers\">import java.io.File\nimport java.net.URL\n\nfun main() {\n    \/\/\u5275\u5efagirl\u8cc7\u6599\u593e\u5230D\u69fd\n    val file = File(\"D:\\\\girl\")\n    if (!file.exists()) {\n        file.mkdirs()\n    }\n\n    val url = URL(\"http:\/\/www.sdxsl.com\/gaoqingtaotu\/\")\n    val html = url.readText()\n    val elements = HTMLParser.getElementsByTag(html, \"img\")\n\n    for (element in elements) {\n        \/\/\u53d6\u51fa\u5716\u7247\u8def\u5f91\n        val src = element.attr(\"src\")\n        \/\/\u53d6\u51fa\u5716\u7247\u540d\u7a31\n        val name = element.attr(\"alt\")\n\n        val file1 = File(\"d:\\\\girl\\\\$name.jpg\")\n        val openConnection = URL(src).openConnection()\n        val bytes = openConnection.getInputStream().readBytes()\n        file1.writeBytes(bytes)\n    }\n    \/\/\u5716\u7247\u5132\u5b58\u5b8c\u6210\n    print(\"OK\")\n}\n<\/code><\/pre>\n<h3>4.\u6548\u679c\u5c55\u793a<\/h3>\n<p><a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/badgameshow.com\/fly\/wp-content\/uploads\/2021\/01\/wp_editor_md_b01e13aac18f426433b1b510ab42e1f7.jpg\"><img decoding=\"async\" src=\"https:\/\/badgameshow.com\/fly\/wp-content\/uploads\/2021\/01\/wp_editor_md_b01e13aac18f426433b1b510ab42e1f7.jpg\" alt=\"\" \/><\/a><\/p>\n\n<div style=\"font-size: 0px; height: 0px; line-height: 0px; margin: 0; padding: 0; clear: both;\"><\/div>","protected":false},"excerpt":{"rendered":"<p>1.\u5c0e\u5165Jsoup dependencies { implementation &#8216;org.jsoup:jsou &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"pgc_sgb_lightbox_settings":"","footnotes":""},"categories":[6],"tags":[169,15,170],"class_list":["post-1110","post","type-post","status-publish","format-standard","hentry","category-kotlin","tag-jsoup","tag-kotlin","tag-170"],"_links":{"self":[{"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/posts\/1110","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/comments?post=1110"}],"version-history":[{"count":2,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/posts\/1110\/revisions"}],"predecessor-version":[{"id":1112,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/posts\/1110\/revisions\/1112"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/media?parent=1110"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/categories?post=1110"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/tags?post=1110"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}