Search code examples
pythonpython-requestspypi

Is there a way to download the source from pypi using a script?


Following the links (Eg: https://pypi.python.org/packages/source/p/py-web-search/py-web-search-0.2.1.tar.gz#md5=4e2f7363acdc1e7c08629edfa7996a5a ) provided on pypi from a browser will allow us to download the source code. Is there a way to do this from a script?

So far I have this:

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)

Note: Opening the file in binary mode causes this error TypeError: 'str' does not support the buffer interface

When I open the tar file using the archive manager it tells that an error occurred while loading the archive.

I tried printing s.text and then redirecting the output to pws.tar.gz but it makes no difference.


Solution

  • It's optional(if you want to download a very large file then you can turn it on)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()