为什么在使用pygithub时出现“403 仓库访问被阻止”异常?

0 投票
1 回答
822 浏览
提问于 2025-04-18 17:53

我正在尝试通过pygithub来抓取GitHub用户喜欢的编程语言,但很奇怪,每次我想抓取用户ELLIOTTCABLE的仓库时,都会遇到以下异常:

Traceback (most recent call last):
File "/home/gf/KuaiPan/code/Python/Data Mining/test.py", line 14, in <module>
repo = user.get_repo(j)
File "/usr/lib/python3.4/site-packages/github/NamedUser.py", line 449, in get_repo
"/repos/" + self.login + "/" + name
File "/usr/lib/python3.4/site-packages/github/Requester.py", line 169, in requestJsonAndCheck
return self.__check(*self.requestJson(verb, url, parameters, headers, input, cnx))
File "/usr/lib/python3.4/site-packages/github/Requester.py", line 177, in __check
raise self.__createException(status, responseHeaders, output)
github.GithubException.GithubException: 403 {'message': 'Repository access blocked', 'block': {'reason': 'unavailable', 'created_at': '2014-01-31T14:32:14-08:00'}}

我的Python代码如下:

#!/usr/bin/env python3
from github import Github


ACCESS_TOKEN = 'my credential'
client = Github(ACCESS_TOKEN, per_page=100)
user = client.get_user('ELLIOTTCABLE')
repo_list = [repo.name for repo in user.get_repos() if not repo.fork]
print(repo_list)

for j in repo_list:
    repo = user.get_repo(j)
    lang = repo.language
    print(j,':',lang)

1 个回答

1

403 HTTP状态表示请求被禁止,也就是说你提供的凭证无法让你访问某些接口。

所以在创建Github对象时,你可能需要提供有效的凭证(用户名/密码):

#!/usr/bin/env python3
from github import Github

ACCESS_USERNAME = 'username'
ACCESS_PWD = "password"
client = Github(ACCESS_USERNAME, ACCESS_PWD, per_page=100)
user = client.get_user('ELLIOTTCABLE')
repo_list = [repo.name for repo in user.get_repos() if not repo.fork]
print(repo_list)

for j in repo_list:
    repo = user.get_repo(j)
    lang = repo.language
    print(j,':',lang)

这样应该就可以正常工作了。

撰写回答