How can I split a url string up into separate parts in Python?

前端 未结 6 964
南笙
南笙 2020-12-13 08:02

I decided that I\'ll learn python tonight :) I know C pretty well (wrote an OS in it) so I\'m not a noob in programming so everything in python seems pretty easy, but I don\

相关标签:
6条回答
  • 2020-12-13 08:25

    The urlparse module in python 2.x (or urllib.parse in python 3.x) would be the way to do it.

    >>> 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'
    >>>
    

    If you wanted to do more work on the path of the file under the url, you can use the posixpath module :

    >>> from posixpath import basename, dirname
    >>> basename(parse_object.path)
    'path.html'
    >>> dirname(parse_object.path)
    '/random/folder'
    

    After that, you can use posixpath.join to glue the parts together.

    EDIT: I totally forgot that windows users will choke on the path separator in os.path. I read the posixpath module docs, and it has a special reference to URL manipulation, so all's good.

    0 讨论(0)
  • 2020-12-13 08:30

    I have no experience with Python, but I found the urlparse module, which should do the job.

    0 讨论(0)
  • 2020-12-13 08:33

    In Python a lot of operations are done using lists. The urlparse module mentioned by Sebasian Dietz may well solve your specific problem, but if you're generally interested in Pythonic ways to find slashes in strings, for example, try something like this:

    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
    

    The output of this program is this:

    slashparts = ['http:', '', 'example.com', 'random', 'folder', 'path.html']
    basename = http://example.com/
    dirname = http://example.com/random/folder/
    

    The interesting bits are split, join, the slice notation array[A:B] (including negatives for offsets-from-the-end) and, as a bonus, the % operator on strings to give printf-style formatting.

    0 讨论(0)
  • You can use python's library furl:

    f = furl.furl("http://example.com/random/folder/path.html")
    print(str(f.path))  # '/random/folder/path.html'
    print(str(f.path).split("/")) # ['', 'random', 'folder', 'path.html']
    

    To access word after first "/", use:

    str(f.path).split("/") # random
    
    0 讨论(0)
  • 2020-12-13 08:39

    Thank you very much to the other answerers here, who pointed me in the right direction via the answers they have given!

    It seems like the posixpath module mentioned by sykora's answer is not available in my Python setup (python 2.7.3).

    As per this article it seems that the "proper" way to do this would be using...

    • urlparse.urlparse and urlparse.urlunparse can be used to detach and reattach the base of the URL
    • The functions of os.path can be used to manipulate the path
    • urllib.url2pathname and urllib.pathname2url (to make path name manipulation portable, so it can work on Windows and the like)

    So for example (not including reattaching the base URL)...

    >>> import urlparse, urllib, os.path
    >>> os.path.dirname(urllib.url2pathname(urlparse.urlparse("http://example.com/random/folder/path.html").path))
    '/random/folder'
    
    0 讨论(0)
  • 2020-12-13 08:42

    If this is the extent of your URL parsing, Python's inbuilt rpartition will do the job:

    >>> URL = "http://example.com/random/folder/path.html"
    >>> Segments = URL.rpartition('/')
    >>> Segments[0]
    'http://example.com/random/folder'
    >>> Segments[2]
    'path.html'
    

    From Pydoc, str.rpartition:

    Splits the string at the last occurrence of sep, and returns a 3-tuple containing the part before the separator, the separator itself, and the part after the separator. If the separator is not found, return a 3-tuple containing two empty strings, followed by the string itself

    What this means is that rpartition does the searching for you, and splits the string at the last (right most) occurrence of the character you specify (in this case / ). It returns a tuple containing:

    (everything to the left of char , the character itself , everything to the right of char)
    
    0 讨论(0)
提交回复
热议问题