Py学习  »  Git

用爬虫写一个Github Trending API

兼葭 • 6 年前 • 343 次点击  

github.png

这学期打算做一个关于GitHub的Android应用,其中一个模块就是查看GitHub当天,当周,当月的热门项目和开发者。其实GitHub给了开发者相当丰富的API接口developer.github.com/v3/,包括认证,搜索,活动等接口,但就是没有提供获取Trending的接口。去GitHubTrending的主页看了一下,并看了其html代码,发现想要的东西都可以用爬虫爬去下来,于是就着手爬去信息。


以下所有请求都为get请求,请求主地址为https://trendings.herokuapp.com

获取热门项目

路径

/repo

参数

名称 类型 描述
lang 字符串 可选,热门项目的语言
since 字符串 可选,get请求参数,无这参数则自动获取当天的热门项目,参数值只有三个,分别是daily,weekly,monthly。

例如请求https://trendings.herokuapp.com/repo?lang=java&since=weekly 返回结果:

//status code: 201
{
  "count": 25,
  "msg": "done",
  //项目集合
  "items": [
    {
      //项目贡献者的头像地址集合
      "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"
      ],
      //项目的地址
      "repo_link": "https://github.com/kdn251/interviews",
      //项目描述
      "desc": "Everything you need to know to get the job.",
      //项目仓库
      "repo": "kdn251/interviews",
      //目前为止的的stars数
      "stars": "5,772",
       //目前为止的forks数
      "forks": "539",
      //项目所属语言
      "lang": "Java",
      //今天或者这周或者这个月的starts数
      "added_stars": "4,591 stars this week"
    },
    .
    .
    .
  ]
}

获取热门开发者

路径

/developer

参数

名称 类型 描述
lang 字符串 可选,热门开发者使用的主要语言
since 字符串 可选,get请求参数,无这参数则自动获取当天的热门开发者,参数值只有三个,分别是daily,weekly,monthly。

请求https://trendings.herokuapp.com/developer?lang=java&since=weekly 返回结果:

//status code: 201
{
  "count": 25,
  "msg": "done",
  //开发者集合
  "items": [
    {
      //开发者在GitHub上的用户名
      "user": "google",
      //开发者在GitHub上的主页链接
      "user_link": "https://github.com/google",
        //开发者的全名
      "full_name": "(Google)",
        // 开发者的头像地址
      "developer_avatar": "https://avatars1.githubusercontent.com/u/1342004?v=3&s=96"
    },
    .
    .
    .
]
}

获取GitHub上的trending语言。

请求地址: https://trendings.herokuapp.com/lang 返回:

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

异常

有时候去访问GitHub的trending时,会找不到热门项目或者开发者的,这时就会返回如下的json。:

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

维护

若某个接口无法调用,请通过简信或者邮箱doforce@126.com与我取得联系,我会尽快修复问题。

GitHub项目地址

今天看啥 - 高品质阅读平台
本文地址:http://www.jintiankansha.me/t/5X8SKKl7GT
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/5374
 
343 次点击