urllib无法正确处理#字符

时间:2015-03-16 21:24:14

标签: python urllib

我的网址如下所示:

http://me:me1234#@localhost:8080/

当我在这个网址上运行urlparse时,而不是netlocpath返回 me:me1234#@ localhost:8080 ,它只返回 me:me1234

from six.moves.urllib import parse
o=parse.urlparse('http://me:me1234#@localhost:8080/')
print o

ParseResult(scheme ='http',netloc ='me:me1234',path ='',params ='',query ='',fragment ='@ localhost:8080 /')

知道为什么它无法解析#?我认为这是一个非常标准的网址。

1 个答案:

答案 0 :(得分:1)

它是fragment。您need to首先对其进行编码:

from six.moves.urllib import parse
o=parse.urlparse('http://me:me1234%23@localhost:8080/')
print o

这应该可以满足您的需求。