How To Split A Web Address
So I'm using python to do some parsing of web pages and I want to split the full web address into two parts. Say I have the address http://www.stackoverflow.com/questions/ask. I wo
Solution 1:
Dan is right: urlparse is your friend:
>>>from urlparse import urlparse>>>>>>parts = urlparse("http://www.stackoverflow.com/questions/ask")>>>parts.scheme + "://" + parts.netloc
'http://www.stackoverflow.com'
>>>parts.path
'/questions/ask'
Note: In Python 3 it's from urllib.parse import urlparse
Solution 2:
Use the Python urlparse module:
https://docs.python.org/library/urlparse.html
For a well-defined and well-traveled problem like this, don't bother with writing your own code, let alone your own regular expressions. They cause too much trouble ;-).
Solution 3:
import re
url = "http://stackoverflow.com/questions/ask"
protocol, domain = re.match(r"(http://[^/]*)(.*)", url).groups()
Post a Comment for "How To Split A Web Address"