用爬蟲(chóng)寫(xiě)一個(gè) GitHub Trending API

github.png

這學(xué)期打算做一個(gè)關(guān)于 GitHub 的 Android 應(yīng)用,其中一個(gè)模塊就是查看 GitHub 當(dāng)天,當(dāng)周,當(dāng)月的熱門(mén)項(xiàng)目和開(kāi)發(fā)者。其實(shí) GitHub 給了開(kāi)發(fā)者相當(dāng)豐富的 API 接口 https://developer.github.com/v3/,包括認(rèn)證,搜索,活動(dòng)等接口,但就是沒(méi)有提供獲取 Trending 的接口。去 GitHub Trending 的主頁(yè)看了一下,并看了其 html 代碼,發(fā)現(xiàn)想要的東西都可以用爬蟲(chóng)爬去下來(lái),于是就著手爬去信息。


以下所有請(qǐng)求都為get請(qǐng)求,請(qǐng)求主地址為https://trendings.herokuapp.com

獲取熱門(mén)項(xiàng)目

路徑

/repo

參數(shù)

名稱(chēng) 類(lèi)型 描述
lang 字符串 可選,熱門(mén)項(xiàng)目的語(yǔ)言
since 字符串 可選,get請(qǐng)求參數(shù),無(wú)這參數(shù)則自動(dòng)獲取當(dāng)天的熱門(mén)項(xiàng)目,參數(shù)值只有三個(gè),分別是daily,weekly,monthly。

例如請(qǐng)求 https://trendings.herokuapp.com/repo?lang=java&since=weekly

返回結(jié)果:

//status code: 201
{
  "count": 25,
  "msg": "done",
  //項(xiàng)目集合
  "items": [
    {
      //項(xiàng)目貢獻(xiàn)者的頭像地址集合
      "avatars": [
        "https://avatars0.githubusercontent.com/u/16903644?v=3&s=40",
        "https://avatars2.githubusercontent.com/u/8622362?v=3&s=40",
        "https://avatars0.githubusercontent.com/u/10773353?v=3&s=40",
        "https://avatars3.githubusercontent.com/u/6392550?v=3&s=40",
        "https://avatars1.githubusercontent.com/u/3837836?v=3&s=40"
      ],
      //項(xiàng)目的地址
      "repo_link": "https://github.com/kdn251/interviews",
      //項(xiàng)目描述
      "desc": "Everything you need to know to get the job.",
      //項(xiàng)目倉(cāng)庫(kù)
      "repo": "kdn251/interviews",
      //目前為止的的stars數(shù)
      "stars": "5,772",
       //目前為止的forks數(shù)
      "forks": "539",
      //項(xiàng)目所屬語(yǔ)言
      "lang": "Java",
      //今天或者這周或者這個(gè)月的starts數(shù)
      "added_stars": "4,591 stars this week"
    },
    .
    .
    .
  ]
}

獲取熱門(mén)開(kāi)發(fā)者

路徑

/developer

參數(shù)

名稱(chēng) 類(lèi)型 描述
lang 字符串 可選,熱門(mén)開(kāi)發(fā)者使用的主要語(yǔ)言
since 字符串 可選,get請(qǐng)求參數(shù),無(wú)這參數(shù)則自動(dòng)獲取當(dāng)天的熱門(mén)開(kāi)發(fā)者,參數(shù)值只有三個(gè),分別是daily,weekly,monthly。

請(qǐng)求 https://trendings.herokuapp.com/developer?lang=java&since=weekly

返回結(jié)果:

//status code: 201
{
  "count": 25,
  "msg": "done",
  //開(kāi)發(fā)者集合
  "items": [
    {
      //開(kāi)發(fā)者在GitHub上的用戶(hù)名
      "user": "google",
      //開(kāi)發(fā)者在GitHub上的主頁(yè)鏈接
      "user_link": "https://github.com/google",
        //開(kāi)發(fā)者的全名
      "full_name": "(Google)",
        // 開(kāi)發(fā)者的頭像地址
      "developer_avatar": "https://avatars1.githubusercontent.com/u/1342004?v=3&s=96"
    },
    .
    .
    .
]
}

獲 取GitHub 上的所有 trending 語(yǔ)言。

請(qǐng)求地址:https://trendings.herokuapp.com/lang

返回結(jié)果:

//status code: 201
{
    "count": 464,
    "items": [
        "1C-Enterprise",
        "ABAP",
        "ABNF",
        "ActionScript",
        "Ada",
        .
        .
        .
        "YANG",
        "Zephir",
        "Zimpl"
    ],
    "msg": "suc"
}

異常

有時(shí)候去訪問(wèn) GitHub 的 trending 時(shí),會(huì)找不到熱門(mén)項(xiàng)目或者開(kāi)發(fā)者的,這時(shí)就會(huì)返回如下的 JSON:

//status code: 404
{
  "msg": "Unavialiable.",
  "count":0,
  "items": []
}

維護(hù)

若某個(gè)接口無(wú)法調(diào)用,請(qǐng)通過(guò)簡(jiǎn)信或者郵箱 doforce@126.com 與我取得聯(lián)系,我會(huì)盡快修復(fù)問(wèn)題。

GitHub項(xiàng)目地址
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容