我想获取 github 用户及其位置。我知道有 Github api(GET/users) 可以为我提供用户列表。目前我正在使用 PyGithub访问 github 但似乎这个库还没有实现这个 API。谁能建议我如何使用任何 github API 库获取 github 用户及其位置?
编辑1: 我更新了如下代码。但有些我无法获取电子邮件 ID 和位置。
import github3
from datetime import datetime
def main():
g = github3.login(username="rakeshcusat", password="mypassword")
for user in g.iter_emails():
print user
current_time = datetime.now()
fhandler = open("githubuser_"+current_time.strftime("%d-%m-%y-%H:%M:%S"), "w")
for user in g.iter_all_users():
fhandler.write(" user: {0}, email: {1}, location: {2}\n".format(user, user.email, user.location))
#fhandler.flush()
fhandler.close()
if __name__ == "__main__":
main()
样本输出
user: andywatts, email: None, location:
user: mueller, email: None, location:
user: cp, email: None, location:
user: davea, email: None, location:
user: vrieskist, email: None, location:
请您参考如下方法:
github3 library支持对所有用户的迭代:
import github3
for user in github3.iter_all_users():
user.refresh()
print user.location
github3.iter_all_users()生产 User objects .
您需要在此处调用 .refresh(),因为 /users endpoint只返回用户信息的一小部分,位置不包括在其中。这需要另一个 API 请求,因此您可能需要调整脚本速度以避免达到 GitHub 速率限制。
github3 的 future 版本(比 0.7.0 更新)增加了对指定页面(批处理)大小的支持,以减少您需要发出的 API 请求的数量; GitHub API 默认为每页 30 个结果,但您可以 load up to 100 results per page相反:
for user in github3.iter_all_users(per_page=100):
user.refresh()
print user.location
