Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- >>> from urllib.parse import urlparse
- >>> url = 'http://example.com/random/folder/path.html'
- >>> parse_object = urlparse(url)
- >>> parse_object.netloc
- 'example.com'
- >>> parse_object.path
- '/random/folder/path.html'
- >>> parse_object.scheme
- 'http'
- >>>
- >>> from posixpath import basename, dirname
- >>> basename(parse_object.path)
- 'path.html'
- >>> dirname(parse_object.path)
- '/random/folder'
- >>> URL = "http://example.com/random/folder/path.html"
- >>> Segments = URL.rpartition('/')
- >>> Segments[0]
- 'http://example.com/random/folder'
- >>> Segments[2]
- 'path.html'
- (everything to the left of char , the character itself , everything to the right of char)
- url = 'http://example.com/random/folder/path.html'
- # Create a list of each bit between slashes
- slashparts = url.split('/')
- # Now join back the first three sections 'http:', '' and 'example.com'
- basename = '/'.join(slashparts[:3]) + '/'
- # All except the last one
- dirname = '/'.join(slashparts[:-1]) + '/'
- print 'slashparts = %s' % slashparts
- print 'basename = %s' % basename
- print 'dirname = %s' % dirname
- split
Add Comment
Please, Sign In to add comment