-1

I would like to download binary data (a .wav file) from a website using python 3.

I was able to get the binary data as a string using requests.get but when I tried to write it to a file, the data was changed and the saved file was not correct:

import requets
waveurl = 'http://www ..... /song.wav'
res = requests.get(waveurl)
with open("song.wav, "wb") as f:
    f.write(bytes(res.text)

I also tried using struct.pack("B",each_byte) but it didn't help.

How can I convert properly download and save binary data to a file without changing it?

2

1 Answer 1

0

Based on the Request documentation you can access the response body as bytes using content instead of text. I would recommend that you give that a try.

import request
waveurl = 'http://www ..... /song.wav'
res = requests.get(waveurl)
with open("song.wav, "wb") as f:
    f.write(res.content)

I haven't tested this option, this i meant to provide a starting point for you.

Sign up to request clarification or add additional context in comments.

3 Comments

thx it solved it yeeey Thx.... but .... still how to take string and chage his meaning as bytes without changes
In your case I would recommend getting the raw bytes, since converting a string to bytes would require you to know the char set. Here is another thread that may be helpful stackoverflow.com/questions/7585435/…. And since it is a related topic you may also want to look into character sets since that is an important part of converting text to bytes joelonsoftware.com/2003/10/08/…
Also, since this solved your question please do consider accepting the answer.

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.