1. package com.example.jsoup.douban
    2. import org.apache.http.client.methods.HttpGet
    3. import org.apache.http.impl.client.HttpClientBuilder
    4. import org.apache.http.util.EntityUtils
    5. import org.jsoup.Jsoup
    6. import org.seimicrawler.xpath.JXDocument
    7. import org.seimicrawler.xpath.JXNode
    8. import java.util.stream.Collectors
    9. fun main() {
    10. val queryMap: Map<String, Int> = mapOf(
    11. "26737158" to 13,
    12. "26821378" to 12,
    13. "26999861" to 13,
    14. "30329046" to 12,
    15. "30446494" to 12,
    16. "35196767" to 24
    17. )
    18. val infos: ArrayList<Info> = ArrayList()
    19. queryMap.forEach {
    20. val subject = it.key
    21. val episodeMax = it.value
    22. for (index in 1..episodeMax) {
    23. infos.add(queryInfo(subject, index))
    24. }
    25. }
    26. infos.forEach { info ->
    27. println("---------------------------\n" +
    28. "序号:${info.index}\n" +
    29. "全名:${info.fullName}\n" +
    30. "中文名:${info.cnName}\n" +
    31. "播放时间:${info.date}\n" +
    32. "简介:\n${info.desc}")
    33. }
    34. }
    35. private fun queryInfo(subject: String, episode: Int): Info {
    36. val html = getHtml(subject, episode)
    37. val document = Jsoup.parse(html)
    38. val jxDocument = JXDocument.create(document)
    39. val cnNameNode = jxDocument.selN("//*[@id=\"content\"]/div/div[1]/ul/li[1]/span[2]")
    40. val cnName = cnNameNode[0].asElement().childNodes()[0].outerHtml()
    41. val dateNode = jxDocument.selN("//*[@id=\"content\"]/div/div[1]/ul/li[3]/span[2]")
    42. val date = dateNode[0].asElement().childNodes()[0].outerHtml()
    43. val descAllNode = jxDocument.selN("//*[@id=\"link-report\"]/span[1]")
    44. val descAll = jxNodesToString(descAllNode)
    45. val descHideNode = jxDocument.selN("//*[@id=\"link-report\"]/span[3]")
    46. val descHide = jxNodesToString(descHideNode)
    47. val fullNameNode = jxDocument.selN("//*[@id=\"content\"]/h1")
    48. val fullName = fullNameNode[0].asElement().childNodes().stream().filter { it.outerHtml().trim().isNotEmpty() }
    49. .map { it.childNodes()[0].outerHtml() }.collect(Collectors.joining())
    50. return Info(episode, fullName, cnName, date, descAll + descHide)
    51. }
    52. fun jxNodesToString(nodes: List<JXNode>?): String =
    53. if (nodes != null && nodes.isNotEmpty()) {
    54. nodes[0].asElement().childNodes().stream().map { it.outerHtml() }
    55. .filter { !it.startsWith("<br") }.collect(Collectors.joining(";"))
    56. .replace(Regex("[\\s\\u3000]+"), "").replace(";", "\n")
    57. } else {
    58. ""
    59. }
    60. fun getHtml(subject: String, episode: Int): String {
    61. val httpClient = HttpClientBuilder.create().build()
    62. val httpGet = HttpGet("https://movie.douban.com/subject/$subject/episode/$episode")
    63. httpGet.addHeader(
    64. "User-Agent",
    65. "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36"
    66. )
    67. val response = httpClient.execute(httpGet)
    68. val responseString = EntityUtils.toString(response.entity)
    69. httpClient.close()
    70. return responseString
    71. }
    72. 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>