rohithpr
rohithpr

Reputation: 6330

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.

Upvotes: 0

Views: 1204

Answers (2)

Ajay
Ajay

Reputation: 5347

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()

Upvotes: 1

Aereaux
Aereaux

Reputation: 855

This post seems to think it would work with opening it in binary mode and using write(bytes(s.text, 'UTF-8')) would work.

Upvotes: 0

Related Questions