如何用urllib或urllib2编码URL
我想把网址从 example.com/page.html
改成类似 example.com/a$xDzf9D84qGBOeXkXNstw%3D%3D106
这样的格式。
2 个回答
2
你可能想要的是这样的:
>>> url = 'stackoverflow.com/questions/2841879/how-to-encode-a-url-with-urllib-or-urllib2'
>>> host, path = url.split('/', 1)
>>> path_mangled = ''.join(['%%%02x' % ord(x) if x not in '/?&' else x for x in path])
>>> url_mangled = '/'.join([host, path_mangled])
>>> url_mangled
'stackoverflow.com/%71%75%65%73%74%69%6f%6e%73/%32%38%34%31%38%37%39/%68%6f%77%2d%74%6f%2d%65%6e%63%6f%64%65%2d%61%2d%75%72%6c%2d%77%69%74%68%2d%75%72%6c%6c%69%62%2d%6f%72%2d%75%72%6c%6c%69%62%32'
(注意,对于带有协议的完整网址(比如http://..),你需要修改第二行)
3
如果你是指这个:
>>> import urllib, base64
>>> urllib.quote_plus('example.com/page.html')
'example.com%2Fpage.html'
>>> base64.urlsafe_b64encode('example.com/page.html')
'ZXhhbXBsZS5jb20vcGFnZS5odG1s'