1

i want to download a subtitle from http://www.turkcealtyazi.org/sub/670264/fantastic-beasts-and-where-to-find-them.html

There is a button that redirects to "down.php" and starts the download.This the is button's html code:

<form  method="post" action="/down.php" >
        <div style="text-align:center">
                <input type="hidden" name="idid" value="670441">
                <input type="hidden" name="altid" value="670264">
                <input type="hidden" name="sidid" value="8a8ed56bafbf7df631e367f1289eb046">

                <button type="submit" class="altIndirButton">
                    <span class="altIndir1"></span>
                </button>
        </div>
</form>

This is my code, it downloads and saves the file without an error but the downloaded zip file is always corrupted.

import requests

def saveDisc(text):
    f = open("subtitle.zip","w")
    f.write(text)
    f.close()

data = {'idid': "670441", 'altid':"670264",'sidid':"8a8ed56bafbf7df631e367f1289eb046"}
response = requests.post('http://www.turkcealtyazi.org/down.php', data=data)

print response.headers.get("Content-disposition")  #output=attachment; filename=670264-Fantastic-Beasts-and-Where-to-Find-Them-2016-1CD-23.976fps-TR-29kB-TurkceAltyazi-org.zip
print len(response.content) #output=30539

saveDisc(response.content)

Why my zip-file is corrupt?

Downloaded files comparison, archive is corrupt

1 Answer 1

1

You are opening the file in w+ mode, so it interprets it as text file!

Python on Windows makes a distinction between text and binary files; the end-of-line characters in text files are automatically altered slightly when data is read or written. This behind-the-scenes modification to file data is fine for ASCII text files, but it’ll corrupt binary data like that in JPEG or EXE files.

Use the mode wb+ for binary files. Like this :

f = open("subtitle.zip","wb+")
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much i was tring to fix this for hours. It took only 30 sec for you, you are awesome.

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.