擅长:python、mysql、java
<p>此行为的原因在wget库中。在它内部,用<code>urllib.parse.quote()</code>(<a href="https://docs.python.org/3/library/urllib.parse.html#urllib.parse.quote" rel="nofollow noreferrer">https://docs.python.org/3/library/urllib.parse.html#urllib.parse.quote</a>)对URL进行编码</p>
<p>基本上,它用相应的<code>%xx</code>转义字符替换字符。您的URL已转义,但库不知道。当解析<code>%20</code>时,它将<code>%</code>视为需要替换的字符,因此结果是<code>%2520</code>和不同的URL-因此出现403错误</p>
<p>您可以先解码该URL,然后将其传递,但随后您将遇到此库的另一个问题,因为您的URL有参数<code>filename*=</code>,但库需要<code>filename=</code></p>
<p>我建议您这样做:</p>
<pre><code># get the file
req = requests.get(parsed_url)
# parse your URL to get GET parameters
get_parameters = [x for x in parsed_url.split('?')[1].split('&')]
filename = ''
# find the get parameter with the name
for get_parameter in get_parameters:
if "filename*=" in get_parameter:
# split it to get the name
filename = get_parameter.split('filename*=')[1]
# save the file
with open(<path> + filename, 'wb') as file:
file.write(req.content)
</code></pre>
<p>我还建议删除该文件名中的<code>utf-8''</code>,因为我认为它实际上不是文件名的一部分。您也可以使用正则表达式获取文件名,但这对我来说更容易</p>