response headers like this:
Content-Disposition:attachment;filename*=charset'lang'value
In my script, temporarily use the custom method to hook function stream._get_filename.
from requests_toolbelt.downloadutils import stream
def _get_filename(content_disposition):
_QUOTED_STRING_RE = r'"[^"\\]*(?:\\.[^"\\]*)*"'
_OPTION_HEADER_PIECE_RE = re.compile(
r';\s*(%s|[^\s;=]+)\s*(?:=\s*(%s|[^;]+))?\s*' % (_QUOTED_STRING_RE,
_QUOTED_STRING_RE)
)
for match in _OPTION_HEADER_PIECE_RE.finditer(content_disposition):
k, v = match.groups()
if k == 'filename':
# ignore any directory paths in the filename
return os.path.split(v)[1]
if k == 'filename*':
return unquote(v).split('\'\'')[1]
return None
stream._get_filename = _get_filename
Expect to fix this issue