Python并强制下载文件

时间:2016-07-27 08:45:13

标签: python python-requests

我目前拥有以下代码,只要GET上的响应具有content-disposition标头,该代码就可以正常运行。但是当没有标题时会出现问题。通常这表示在浏览器中查看文件。有没有办法在python中强制下载?

def action_download(doc_list):
    i = 0
    for doc in doc_list:
        try:
            r = requests.get(doc)
            filename = re.search('filename="(.*)"', r.headers['content-disposition']).group(1)
            with open(filename, "w") as code:
                i += 1
                code.write(r.content)
                code.close()
        except requests.exceptions.ConnectionError:
            print '\nError: File Not Found Server Side'
            print doc
        except Exception:
            print '\nUnknown Error'
            print doc
            print r.headers
            traceback.print_exc()
            continue
    if i < 1:
        sys.exit()

基于响应的更新代码

定义文件名并通过hte write fucntion传递数据内容非常有效。对于遇到此问题的任何人,请在下面更新了代码。

*请注意KeyError例外中的添加。

def action_download(doc_list):
    i = 0
    for doc in doc_list:
        try:
            r = requests.get(doc)
            filename = re.search('filename="(.*)"', r.headers['content-disposition']).group(1)
            with open(filename, "w") as code:
                i += 1
                code.write(r.content)
                code.close()
        except requests.exceptions.ConnectionError:
            print '\nError: File Not Found Server Side'
            print doc
        except KeyError:
            temp = str(doc).rsplit('.', 1)[1]
            ext = re.sub(r'\?.*', r'', temp)
            filename = "file{}.{}".format(i, ext.replace('?T', ''))
            with open(filename, "w") as code:
                i += 1
                code.write(r.content)
                code.close()
            continue
        except Exception:
            print '\nUnknown Error'
            print doc
            print r.headers
            traceback.print_exc()
            continue
    if i < 1:
        sys.exit()

0 个答案:

没有答案