Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/github/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x Python:在试图查找Github存储库的分叉时,会出现无休止的递归_Python 3.x_Github_Recursion_Python Requests_Fork - Fatal编程技术网

Python 3.x Python:在试图查找Github存储库的分叉时,会出现无休止的递归

Python 3.x Python:在试图查找Github存储库的分叉时,会出现无休止的递归,python-3.x,github,recursion,python-requests,fork,Python 3.x,Github,Recursion,Python Requests,Fork,作为一名学习Python3和如何使用API的初学者,我正在尝试开发一个简单的脚本,该脚本使用Github存储库并递归地标识所述存储库中所有分支的所有分支。这个想法是以一个列表类型变量结束,该变量包含fork(以及关于这些fork的父级的信息)(除非另一个变量类型更好?),我可以将其导出到JSON文件作为一个附加练习 现在,我正在使用Python 3请求库以及for和while循环的组合(是的,我知道while=True循环是危险的,请告诉我是否有更好的方法) 下面是一段代码,其中包含一个示例存储

作为一名学习Python3和如何使用API的初学者,我正在尝试开发一个简单的脚本,该脚本使用Github存储库并递归地标识所述存储库中所有分支的所有分支。这个想法是以一个
列表
类型变量结束,该变量包含fork(以及关于这些fork的父级的信息)(除非另一个变量类型更好?),我可以将其导出到JSON文件作为一个附加练习

现在,我正在使用Python 3
请求
库以及
for
while
循环的组合(是的,我知道
while=True
循环是危险的,请告诉我是否有更好的方法)

下面是一段代码,其中包含一个示例存储库(),用于在Github身份验证令牌模糊的情况下查找fork:

import requests

#
# getting forks
#

# the starting repository to find forks for
username = "OpenROV"
repo = "openrov-software"
# fake Github auth token:
auth = ("ScroogeMcDuck", "fqo4p6xx6p2cgnwi3fakjotx7c46n5xcfzxpz3hz")

# initialise an empty list of forks
forks = list()

# Recursive function to find all forks of forks of a Github repository
def get_Github_forks(owner, repo, forks, auth=None, recurse=False):
    page_counter = 1
    request_output = list()
    print("Finding forks for repository: {}/{}".format(owner, repo))
    while True:
        print("Retrieving forks page {}".format(page_counter))
        request_string = "https://api.github.com/repos/{}/{}/forks?page={}".format(owner, repo, page_counter)
        print("Request string:", request_string)
        request_output = requests.get(request_string, auth = auth).json()
        for item in request_output:
            forks.append({"fork_owner": item["owner"]["login"], "fork_repo": item["name"], "created_at": item["created_at"], "forked_from_owner": owner, "forked_from_repo": repo})
        # Stop going to the next page of results if the current result is 0,
        # meaning we're already past the last page.
        if len(request_output) == 0:
            break
        # Otherwise, move on to the next page of results
        else:
            page_counter = page_counter + 1
    for fork in forks:
        get_Github_forks(owner=fork["fork_owner"], repo=fork["fork_repo"], forks=forks, auth=auth, recurse=True)

# find forks
get_Github_forks(owner=username, repo=repo, forks=forks, auth=auth, recurse=True)
在运行这段代码时,我看到它成功地识别了父/原始存储库的所有分支(它有179个分支),但它无法重复获取第一个分支的第一页结果,即
labimage/openrov software
。请注意,第一个分叉(
labimage/openrov软件
)实际上没有任何分叉。如果我使用命令
curl-I“https://api.github.com/repos/labimage/openrov-software/forks?page=1“
在终端中,它不显示任何结果

下面是运行此脚本时无休止的输出的顶部,您可以看到它在哪里卡住了:

Finding forks for repository: OpenROV/openrov-software
Retrieving forks page 1
Request string: https://api.github.com/repos/OpenROV/openrov-software/forks?page=1
Retrieving forks page 2
Request string: https://api.github.com/repos/OpenROV/openrov-software/forks?page=2
Retrieving forks page 3
Request string: https://api.github.com/repos/OpenROV/openrov-software/forks?page=3
Retrieving forks page 4
Request string: https://api.github.com/repos/OpenROV/openrov-software/forks?page=4
Retrieving forks page 5
Request string: https://api.github.com/repos/OpenROV/openrov-software/forks?page=5
Retrieving forks page 6
Request string: https://api.github.com/repos/OpenROV/openrov-software/forks?page=6
Retrieving forks page 7
Request string: https://api.github.com/repos/OpenROV/openrov-software/forks?page=7
Finding forks for repository: labimage/openrov-software
Retrieving forks page 1
Request string: https://api.github.com/repos/labimage/openrov-software/forks?page=1
Finding forks for repository: labimage/openrov-software
Retrieving forks page 1
Request string: https://api.github.com/repos/labimage/openrov-software/forks?page=1
Finding forks for repository: labimage/openrov-software
Retrieving forks page 1
Request string: https://api.github.com/repos/labimage/openrov-software/forks?page=1
Finding forks for repository: labimage/openrov-software
Retrieving forks page 1
Request string: https://api.github.com/repos/labimage/openrov-software/forks?page=1
Finding forks for repository: labimage/openrov-software
Retrieving forks page 1
Request string: https://api.github.com/repos/labimage/openrov-software/forks?page=1
...
[goes on forever]
...

我做错了什么?这是我第一个Python问题,所以提前感谢您的指导。如果我能提供更多细节,请告诉我。

你不认为这会永远持续下去,因为没有合适的基本条件吗?@Solen'ya感谢你的回答,但很抱歉,作为一个刚刚开始学习的人,我提出了一个无知的问题:没有适当的基本条件是什么意思?我在使用一个软件时解决了类似的问题。我必须找出存储在分支文件夹中的不同类型的标签(需要另一个讲座)。分支结构不是固定的。文件夹可以包含子文件夹和标记。但是标签不能保存文件夹。这是我的基本条件。使用此条件,无论文件夹的结构如何,我都可以找到所有标记。也许您需要识别这个独特的条件,它可以区分fork和repo,并使用递归,而不使用无限循环,以便在到达终点时递归中断。