简体   繁体   中英

python - Downloading file from website using requests

I'm trying to download mp3 files from https://www.alain-pennec.bzh/editions/livre-mp3/mp3/ with the following code:

import requests

def main():
    for i in range(0, 226) :
        URL = "https://www.alain-pennec.bzh/editions/livre-mp3/mp3/AP"+numberOn3Char(i)+".mp3"
        response = requests.get(URL)
        open("AP"+numberOn3Char(i)+".mp3", "wb").write(response.content)
        print(numberOn3Char(i)+" OK")

def numberOn3Char(i):
    ret = str(i)
    if len(ret) == 1 :
        ret = '00' + ret
    elif len(ret) == 2 :
        ret = '0' + ret
    return ret

main()

My issue is that nothing is being downloaded. I get no error and I don't understand why it's not working.

Try changing

open("AP"+numberOn3Char(i)+".mp3", "wb").write(response.content)

to

 name = "AP"+numberOn3Char(i)+".mp3"
 with open(name, 'wb') as f:
    f.write(response.content)

and see if it works.

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