简体   繁体   中英

download multiple pdf using python

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

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM