我想从 http://www.turkcealtyazi.org/sub/670264/fantastic-beasts-and-where-to-find-them.html 下载字幕
有一个按钮可以重定向到“down.php”并开始下载。这是按钮的 html 代码:
<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>
这是我的代码,它下载并保存文件时没有错误,但下载的 zip 文件总是损坏。
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)
为什么我的 zip 文件已损坏?
最佳答案
您正在以 w+ 模式打开文件,因此它会将其解释为文本文件!
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.
对二进制文件使用 wb+ 模式。 像这样:
f = open("subtitle.zip","wb+")
关于python - 使用 Python 从 php 页面获取文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42609491/