guri guri - 22 days ago 7
Python Question

Removing HTTP and WWW from URL python

url1='www.google.com'
url2='http://www.google.com'
url3='http://google.com'
url4='www.google'
url5='http://www.google.com/images'
url6='https://www.youtube.com/watch?v=6RB89BOxaYY


How to strip
http(s)
and
www
from url in Python?

Answer

you can use regex

url = 'http://www.google.com/images'
url = url.replace("http://www.","")
print url

or you can use regular expressions

import re
url = re.compile(r"https?://(www\.)?")
url.sub('', 'http://www.google.com/images').strip().strip('/')