Search code examples
pythonurlpython-3.6urlliburlparse

How do you extract parameters from a url that only contain slash using python?


I have an url "http://example.com/title/hello/users/123/example-1". I would like to extract the information Title: "hello", users": "123" as well as "example-1". How can i use urllib to extract these info? I do not want to use regex for this.

from urllib.parse import urlparse

url = 'http://example.com/title/hello/users/123/example-1'
print(urlparse(url))

# How do i extract the parameters in the path below?
# ParseResult(scheme='http', netloc='example.com', path='/title/hello/users/123/example-1', params='', query='', fragment='')


Solution

  • from urllib.parse import urlparse
    
    parsed = urlparse('http://example.com/title/hello/users/123/example-1')
    parsed = parsed.path.split("/")
    
    

    Urlparse returns a parsed object. We can use the path of this parser object and split it by "/". Here is the result :

    ['', 'title', 'hello', 'users', '123', 'example-1']