Python: Get URL path sections

后端 未结 7 1626
长发绾君心
长发绾君心 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条回答
  •  自闭症患者
    2020-12-01 05:47

    A combination of urlparse and os.path.split will do the trick. The following script stores all sections of a url in a list, backwards.

    import os.path, urlparse
    
    def generate_sections_of_url(url):
        path = urlparse.urlparse(url).path
        sections = []; temp = "";
        while path != '/':
            temp = os.path.split(path)
            path = temp[0]
            sections.append(temp[1])
        return sections
    

    This would return: ["else", "something", "hithere"]

提交回复
热议问题