Python3 怎样将下载链接转成文字
发布网友
发布时间:2022-04-26 23:43
我来回答
共2个回答
热心网友
时间:2022-06-20 08:25
我只能说这是一个Python3.4以上版本的bug,至今还没有在版本中解决。
具体情况可以查下issue17214
Title: http.client.HTTPConnection.putrequest encode error
Type:behavior
Stage:patch review
Components:Library (Lib)
Versions:Python 3.6, Python 3.5, Python 3.4
不过幸运的是已经给出了Patch,可以自行修改,修改方法如下:
diff --git a/Lib/http/client.py b/Lib/http/client.py
--- a/Lib/http/client.py
+++ b/Lib/http/client.py
@@ -263,6 +263,7 @@
headers.append(line)
if line in (b'\r\n', b'\n', b''):
break
+ print(headers)
hstring = b''.join(headers).decode('iso-8859-1')
return email.parser.Parser(_class=_class).parsestr(hstring)
diff --git a/Lib/urllib/request.py b/Lib/urllib/request.py
--- a/Lib/urllib/request.py
+++ b/Lib/urllib/request.py
@@ -652,11 +652,13 @@
"%s - Redirection to url '%s' is not allowed" % (msg, newurl),
headers, fp)
- if not urlparts.path:
- urlparts = list(urlparts)
- urlparts[2] = "/"
- newurl = urlunparse(urlparts)
-
+ urlpartslist = list(urlparts)
+ path = urlparts.path if urlpaths.path else "/"
+ # parse_headers() decodes from iso-8859-1 and unquotes, undo damage
+ path = urlparts.path.encode("iso-8859-1")
+ urlpartslist[2] = quote(path)
+
+ newurl = urlunparse(urlpartslist)
newurl = urljoin(req.full_url, newurl)
# XXX Probably want to forget about the state of the current
热心网友
时间:2022-06-20 08:25
urllib.urlretrieve()方法,下载文件用
不要追问我怎么用,用种东西叫文档
url = "
urllib.urlretrieve(url, '1.rar')