有没有办法使用脚本从pypi下载源代码?

时间:2015-05-29 15:35:01

标签: python python-requests pypi

通过浏览器在pypi上提供的链接(例如:https://pypi.python.org/packages/source/p/py-web-search/py-web-search-0.2.1.tar.gz#md5=4e2f7363acdc1e7c08629edfa7996a5a)将允许我们下载源代码。 有没有办法从脚本中执行此操作?

到目前为止,我有这个:

import requests
s = requests.get('https://pypi.python.org/packages/source/p/py-web-search/py-web-search-0.2.1.tar.gz#md5=4e2f7363acdc1e7c08629edfa7996a5a')
with open('pws.tar.gz', 'w') as fp:
    fp.write(s.text)

注意:以二进制模式打开文件会导致此错误TypeError: 'str' does not support the buffer interface

当我使用存档管理器打开tar文件时,它会在加载存档时发出错误。

我尝试打印s.text然后将输出重定向到pws.tar.gz,但没有区别。

2 个答案:

答案 0 :(得分:1)

它是可选的(如果你想下载一个非常大的文件,那么你可以打开它)stream=True

import requests
s = requests.get('https://pypi.python.org/packages/source/p/py-web-search/py-web-search-0.2.1.tar.gz#md5=4e2f7363acdc1e7c08629edfa7996a5a',stream=True)

with open('pws.tar.gz', 'wb') as fp:
    for chunk in s.iter_content(): 
        if chunk:
            fp.write(chunk)
            fp.flush()

答案 1 :(得分:0)

This post似乎认为在二进制模式下打开它并使用projectID --------- 511 (matches all three) 456 (matches two) 789 (matches one) 会起作用。

相关问题