package com.example.jsoup.doubanimport org.apache.http.client.methods.HttpGetimport org.apache.http.impl.client.HttpClientBuilderimport org.apache.http.util.EntityUtilsimport org.jsoup.Jsoupimport org.seimicrawler.xpath.JXDocumentimport org.seimicrawler.xpath.JXNodeimport java.util.stream.Collectorsfun main() { val queryMap: Map<String, Int> = mapOf( "26737158" to 13, "26821378" to 12, "26999861" to 13, "30329046" to 12, "30446494" to 12, "35196767" to 24 ) val infos: ArrayList<Info> = ArrayList() queryMap.forEach { val subject = it.key val episodeMax = it.value for (index in 1..episodeMax) { infos.add(queryInfo(subject, index)) } } infos.forEach { info -> println("---------------------------\n" + "序号:${info.index}\n" + "全名:${info.fullName}\n" + "中文名:${info.cnName}\n" + "播放时间:${info.date}\n" + "简介:\n${info.desc}") }}private fun queryInfo(subject: String, episode: Int): Info { val html = getHtml(subject, episode) val document = Jsoup.parse(html) val jxDocument = JXDocument.create(document) val cnNameNode = jxDocument.selN("//*[@id=\"content\"]/div/div[1]/ul/li[1]/span[2]") val cnName = cnNameNode[0].asElement().childNodes()[0].outerHtml() val dateNode = jxDocument.selN("//*[@id=\"content\"]/div/div[1]/ul/li[3]/span[2]") val date = dateNode[0].asElement().childNodes()[0].outerHtml() val descAllNode = jxDocument.selN("//*[@id=\"link-report\"]/span[1]") val descAll = jxNodesToString(descAllNode) val descHideNode = jxDocument.selN("//*[@id=\"link-report\"]/span[3]") val descHide = jxNodesToString(descHideNode) val fullNameNode = jxDocument.selN("//*[@id=\"content\"]/h1") val fullName = fullNameNode[0].asElement().childNodes().stream().filter { it.outerHtml().trim().isNotEmpty() } .map { it.childNodes()[0].outerHtml() }.collect(Collectors.joining()) return Info(episode, fullName, cnName, date, descAll + descHide)}fun jxNodesToString(nodes: List<JXNode>?): String = if (nodes != null && nodes.isNotEmpty()) { nodes[0].asElement().childNodes().stream().map { it.outerHtml() } .filter { !it.startsWith("<br") }.collect(Collectors.joining(";")) .replace(Regex("[\\s\\u3000]+"), "").replace(";", "\n") } else { "" }fun getHtml(subject: String, episode: Int): String { val httpClient = HttpClientBuilder.create().build() val httpGet = HttpGet("https://movie.douban.com/subject/$subject/episode/$episode") httpGet.addHeader( "User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36" ) val response = httpClient.execute(httpGet) val responseString = EntityUtils.toString(response.entity) httpClient.close() return responseString}data class Info(val index: Int, val fullName: String, val cnName: String, val date: String, val desc: String)
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.13</version>
</dependency>
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.14.1</version>
</dependency>
<dependency>
<groupId>cn.wanghaomiao</groupId>
<artifactId>JsoupXpath</artifactId>
<version>2.5.0</version>
</dependency>