Pythonic URL Parsing
There are several 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 filename, and the request numbers.
http://www.somesite.com/base/first/second/third/fourth/foo.html?abc=123
should be analyzed:
netloc = 'www.somesite.com'
baseURL = 'base'
path = '/first/second/third/fourth/'
file = 'foo.html?abc=123'
The code below gives the correct result, but is there a 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
a source to share
Since your requirements for what you want are different from what urlparse gives you, which is as good as it is going to get. However, you can replace this:
partCount = len(pathParts) - 1
path = "/"
for i in range(2, partCount):
path += pathParts[i] + "/"
Wherein:
path = '/'.join(pathParts[2:-1])
a source to share
I would tend to start with urlparse
. Alternatively, you can use rsplit
both maxsplit
parameter split
and rsplit
to simplify things:
_, netloc, path, _, q, _ = urlparse(url)
_, base, path = path.split('/', 2) # 1st component will always be empty
path, file = path.rsplit('/', 1)
if q: file += '?' + q
a source to share