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

前端 未结 6 983
南笙
南笙 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.

提交回复
热议问题