Search code examples
pythonurlparse

Using urlparse to remove a certain string?


I have this URL:

www.domain.com/a/b/c/d,authorised=false.html

and I want to convert it into

www.domain.com/a/b/c/d.html

Please note I am using python 2.7.

from urlparse import urlparse

url = "www.domain.com/a/b/c/d,athorised=false.html_i_location=http%3A%2F%2Fwww.domain.com%2Fcms%2Fs%2F0%2Ff416e134-2484-11e4-ae78-00144feabdc0.html%3Fsiteedition%3Dintl&siteedition=intl&_i_referer=http%3A%2F%2Fwww.domain.com%2Fhome%2Fus"

o = urlparse(url)
url = o.hostname + o.path
print url

returns www.domain.com/a/b/c/d,authorised=false.html but I don't know how to remove authorised=false part from the URL


Solution

  • import re
    print re.sub(r',.+\.', '.', 'www.domain.com/a/b/c/d,authorised=false.html')
    
    # www.domain.com/a/b/c/d.html