0

i try this code to download many pdf files:

import requests

for i in range(1, 30):
    r = requests.get('http://www.setpp.kemenkeu.go.id/risalah/ambilFileDariDisk/'+str(i), allow_redirects=True)

    open('file-risalah.pdf', 'wb').write(r.content)

but it only outputs a single pdf file. how to download each pdf file so that it can output as many file as in the iteration? i tied:

import requests

for i in range(1, 30):
    r = requests.get('http://www.setpp.kemenkeu.go.id/risalah/ambilFileDariDisk/'+str(i), allow_redirects=True)

    open('file-risalah'+str[i]+'.pdf', 'wb').write(r.content)

but its error

1
  • You're almost there! str[i] is not the correct syntax though. You used the correct syntax in requests.get('...ambilFileDariDisk/'+str(i), ...) Commented May 20, 2020 at 6:59

2 Answers 2

1

You have to change the name of the saved file each time.

open('file-risalah_'+str(i)+'.pdf', 'wb').write(r.content)
Sign up to request clarification or add additional context in comments.

Comments

0

Is that done when you download each pdf ?

Else you can try this

r = requests.get(str('http://www.setpp.kemenkeu.go.id/risalah/ambilFileDariDisk/'+str(i), ".pdf"), allow_redirects=True)

It's potentially the problem but I don't think.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.