Python: Get URL path sections

后端 未结 7 1628
长发绾君心
长发绾君心 2020-12-01 05:15

How do I get specific path sections from a url? For example, I want a function which operates on this:

http://www.mydomain.com/hithere?image=2934

7条回答
  •  旧时难觅i
    2020-12-01 05:38

    Note in Python3 import has changed to from urllib.parse import urlparse See documentation. Here is an example:

    >>> from urllib.parse import urlparse
    >>> url = 's3://bucket.test/my/file/directory'
    >>> p = urlparse(url)
    >>> p
    ParseResult(scheme='s3', netloc='bucket.test', path='/my/file/directory', params='', query='', fragment='')
    >>> p.scheme
    's3'
    >>> p.netloc
    'bucket.test'
    >>> p.path
    '/my/file/directory'
    

提交回复
热议问题