python - 使用 Python 从 php 页面获取文件

标签 python

我想从 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 文件已损坏?

Downloaded files comparison, archive is corrupt

最佳答案

您正在以 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/

相关文章:

python - Django 管理性能问题

python - SparkPost:至少需要一个有效的收件人 python/django API

python - 将 Tkinter Text 小部件内容保存到具有样式功能的 .txt 文件中

Python:函数在 for 循环中未接收值

python - 将切片索引存储为对象

python - 为什么这是一个糟糕的冒泡排序算法?

python - 如何使 PDF 可通过边栏中的文本进行搜索?

Python - 在冒泡排序中使用元组交换时的空间复杂度是多少?

python - 使用 Pandas 转换时间序列数组中包含周期的数据帧

python - Django:模板中的变量中的变量