org.jsoup jsoup 1.12.1
当前位置:   article > 正文

Kotlin爬取码云(Gitee)首页推荐仓库_码云首页

码云首页
用到的库

Gradle

implementation("org.jsoup:jsoup:1.12.1")
  • 1

Maven

<dependency>
	<groupId>org.jsoup</groupId>
	<artifactId>jsoup</artifactId>
	<version>1.12.1</version>
</dependency>
  • 1
  • 2
  • 3
  • 4
  • 5
代码如下
package org.napolun.easyapi.code

import cn.hutool.core.util.StrUtil
import org.jsoup.Jsoup

/**
 * gitee
 *
 * @author              chenxc
 * @email               997909544@qq.com
 * @Date                2019/11/25  11:40
 */
object Gitee {

    private const val GITEE_HOST = "https://gitee.com/"
    private const val GITEE_REPO = GITEE_HOST + "explore/all?lang={}&order={}&page={}"

    /**
     * 获得gitee trending
     *
     * @param {String} language 语言
     * @param {String} order  recommend starred latest
     */
    fun trending(language: String, order: String, page: Int): MutableCollection<TrendingResult> {
        var doc = Jsoup.connect(StrUtil.format(GITEE_REPO, language, order, page)).get()
        var list = doc.select(".explore-repo__list .item")
        var resultList: MutableCollection<TrendingResult> = mutableListOf()
        for (item in list) {
            var trendingResult = TrendingResult()
            var titleArr = item.select(".project-namespace-path").attr("title").split("/")
            var hrefArr = item.select(".project-namespace-path").attr("href").split("/")
            trendingResult.title = titleArr[1]
            trendingResult.description = item.select(".project-desc").text()
            trendingResult.url = GITEE_HOST + hrefArr[1] + "/" + hrefArr[2]
            trendingResult.coverUrl = item.select(".project-creator-link img").attr("src")
            trendingResult.author = titleArr[0]
            trendingResult.authorUrl = GITEE_HOST + hrefArr[1]
            var social = item.select(".explore-project__meta-social")[0].getElementsByTag("a")
            if (social.size == 2) {
                trendingResult.stars = social[1].getElementsByTag("span").text()
            }
            if (social.size == 3) {
                trendingResult.forks = social[2].getElementsByTag("span").text()
            }
            trendingResult.language = item.select(".project-meta")[0]
                    .getElementsByTag("a")[0].text()
            trendingResult.updateTime = item.select(".timeago").text()
            trendingResult.readmeUrl = GITEE_HOST + hrefArr[1] + "/" + hrefArr[2] + "/raw/master/README.md";
            resultList.add(trendingResult)
        }
        return resultList
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53

参数:

  • language: 可选, 语言, 默认全部
  • order: 可选, 排序, 默认 starred, 全部: recommend, starred and latest.
  • page: 必填, 页码

返回值:

[
  {
      "author":"程序猿DD",
      "authorUrl":"https://gitee.com/didispace",
      "coverUrl":"https://avatar.gitee.com/uploads/88/437188_didispace.png!avatar60?1548556333",
      "createTime":"",
      "description":"Spring Boot基础教程,Spring Boot 2.x版本连载中!!!",
      "forks":"4.9K",
      "language":"Java",
      "readmeUrl":"https://gitee.com/didispace/SpringBoot-Learning/raw/master/README.md",
      "stars":"",
      "title":"SpringBoot-Learning",
      "updateTime":"29天前",
      "url":"https://gitee.com/didispace/SpringBoot-Learning"
  }
]
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

上述代码,均已上传github,地址 https://github.com/chenxxcc/EasyAPI

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/674935
推荐阅读
相关标签