【问题标题】:Getting a corupt zip file when download a zip extension using requests with Python使用 Python 请求下载 zip 扩展时获取损坏的 zip 文件
【发布时间】:2020-07-23 05:05:33
【问题描述】:

我想用 python 脚本在 [链接不再存在] 下载一个测试文件。

如果你自己去链接,下载会自动开始。

虽然,如果我尝试运行这个脚本,我会得到一个损坏的 folder.zip 文件,它是一个 html 文件:

import os
import requests

URL = "http://kolterdev.epizy.com/dir/VoidShipsUploads/folder.zip"
DIR = os.getcwd()

download = requests.get(URL)

print(download.content)

open(filename, 'wb').write(download.content)

这是我找到的 html 文件,而不是 zip 文件:

<html>

<body>
    <script type="text/javascript" src="/aes.js"></script>
    <script>
        function toNumbers(d) {
            var e = [];
            d.replace(/(..)/g, function(d) {
                e.push(parseInt(d, 16))
            });
            return e
        }

        function toHex() {
            for (var d = [], d = 1 == arguments.length && arguments[0].constructor == Array ? arguments[0] : arguments, e = "", f = 0; f < d.length; f++) e += (16 > d[f] ? "0" : "") + d[f].toString(16);
            return e.toLowerCase()
        }
        var a = toNumbers("f655ba9d09a112d4968c63579db590b4"),
            b = toNumbers("98344c2eee86c3994890592585b49f80"),
            c = toNumbers("7a2b1adb5e9b4162d6571ff0b6145a53");
        document.cookie = "__test=" + toHex(slowAES.decrypt(c, 2, a, b)) + "; expires=Thu, 31-Dec-37 23:55:55 GMT; path=/";
        location.href = "http://kolterdev.epizy.com/dir/VoidShipsUploads/folder.zip?i=1";
    </script><noscript>This site requires Javascript to work, please enable Javascript in your browser or use a browser with Javascript support</noscript>
</body>

</html>

如何获取文件?

编辑:具有该 url 的页面不再存在。

【问题讨论】:

    标签: python file download python-requests


    【解决方案1】:
    import urllib.request
    ...
    url = 'http://example.com/'
    response = urllib.request.urlopen(url)
    data = response.read()      # a `bytes` object
    text = data.decode('utf-8') # a `str`; this step can't be used if data is binary
    

    另外,请看一下这个答案 https://stackoverflow.com/a/7244263/13227971

    【讨论】:

    • 这仍然给了我 html 的东西
    【解决方案2】:

    bash 版本

    import os
    url = "http://kolterdev.epizy.com/dir/VoidShipsUploads/folder.zip"
    os.system("wget {}".format(url))
    

    您可以使用 -O 指定位置

    【讨论】:

    • 遗憾的是我不能这样做,因为我也需要能够从 Windows 使用脚本
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多