How can I normalize a URL in python

2019-01-04 08:06发布

I'd like to know do I normalize a URL in python.

For example, If I have a url string like : "http://www.example.com/foo goo/bar.html"

I need a library in python that will transform the extra space (or any other non normalized character) to a proper URL.

8条回答
仙女界的扛把子
2楼-- · 2019-01-04 08:48
import urlparse, urllib
def myquote(url):
    parts= urlparse.urlparse(url)
    return urlparse.urlunparse(parts[:2] + urllib.quote(parts[2]) + parts[3:])

This quotes only the path component.

Otherwise, you could do: urllib.quote(url, safe=":/")

查看更多
ゆ 、 Hurt°
3楼-- · 2019-01-04 08:50

Valid for Python 3.5:

import urllib.parse

urllib.parse.quote([your_url], "\./_-:")

example:

import urllib.parse

print(urllib.parse.quote("http://www.example.com/foo goo/bar.html", "\./_-:"))

the output will be http://www.example.com/foo%20goo/bar.html

Font: https://docs.python.org/3.5/library/urllib.parse.html?highlight=quote#urllib.parse.quote

查看更多
登录 后发表回答