How to download a file using python in a 'smarter' way?

前端 未结 5 1606
盖世英雄少女心
盖世英雄少女心 2020-11-27 10:04

I need to download several files via http in Python.

The most obvious way to do it is just using urllib2:

import urllib2
u = urllib2.urlopen(\'http:/         


        
5条回答
  •  臣服心动
    2020-11-27 10:23

    2 Kender:

    if localName[0] == '"' or localName[0] == "'":
        localName = localName[1:-1]
    

    it is not safe -- web server can pass wrong formatted name as ["file.ext] or [file.ext'] or even be empty and localName[0] will raise exception. Correct code can looks like this:

    localName = localName.replace('"', '').replace("'", "")
    if localName == '':
        localName = SOME_DEFAULT_FILE_NAME
    

提交回复
热议问题