0

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.

rohithpr
  • 6,050
  • 8
  • 36
  • 60

2 Answers2

1

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()
Ajay
  • 5,267
  • 2
  • 23
  • 30
0

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

Aereaux
  • 845
  • 1
  • 8
  • 20