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 下载raw.githubusercontent.com文件的速度非常慢_Python_Github_Github Api - Fatal编程技术网

Python 下载raw.githubusercontent.com文件的速度非常慢

Python 下载raw.githubusercontent.com文件的速度非常慢,python,github,github-api,Python,Github,Github Api,我正在用python 3构建一个应用程序,它需要从raw.githubusercontent.com下载一大堆*.java文件。基本上,我使用GitHub的API v3获取给定存储库中以“.java”结尾的所有路径,然后通过raw.githubusercontent.com下载它们。问题是速度非常慢(

我正在用python 3构建一个应用程序,它需要从raw.githubusercontent.com下载一大堆*.java文件。基本上,我使用GitHub的API v3获取给定存储库中以“.java”结尾的所有路径,然后通过raw.githubusercontent.com下载它们。问题是速度非常慢(<10 kB/s)。现在,它有时以一个合适的速率(40-50kb/s)开始,但随后它通常会很快下降

我已尝试使用requests.Session()保持持久连接。我还尝试使用授权令牌,这是有人建议的。这两种情况都没有改善

我的代码是这样的:

with requests.Session() as s:
    path_index = ""
    for path in paths.splitlines():
        file_url = githubusercontent_prefix + path
        filename = path.split("/")[-1]
        res = s.get(file_url, stream=True, allow_redirects=True)
        outf = open("sources/" + filename, 'w')
        outf.write(res.text)
        outf.close()

考虑使用Python Git库来从RePO中取出文件,而不是从GithuBuffelCyth.com获取文件。@ AdilB,这基本上是我最终所做的。我用wget做了一些实验,我注意到如果你试着下载同一组文件两次,第二次就会快得多。所以githubusercontent.com可能会对文件进行某种缓存。使用“git clone”的速度要快得多,即使其中只有一小部分包含*.java文件。太棒了-是的,我认为使用git库是最有意义的,因为这是GitHub的主要用途:)