There are a number of questions about how to parse a URL in Python, this question is about the best or most Pythonic way to do it.
In my parsing I need 4 parts: the network location, the first part of the URL, the path and the filename and querystring parts.
http://www.somesite.com/base/first/second/third/fourth/foo.html?abc=123
should parse into:
netloc = 'www.somesite.com'
baseURL = 'base'
path = '/first/second/third/fourth/'
file = 'foo.html?abc=123'
The code below produces the correct result, but is there are better way to do this in Python?
url = "http://www.somesite.com/base/first/second/third/fourth/foo.html?abc=123"
file= url.rpartition('/')[2]
netloc = urlparse(url)[1]
pathParts = path.split('/')
baseURL = pathParts[1]
partCount = len(pathParts) - 1
path = "/"
for i in range(2, partCount):
path += pathParts[i] + "/"
print 'baseURL= ' + baseURL
print 'path= ' + path
print 'file= ' + file
print 'netloc= ' + netloc