如何使用 python 从仅包含斜杠的 url 中提取参数?

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

我有一个 url“http://example.com/title/hello/users/123/example-1”。我想提取信息 Title: "hello", users": "123" 以及 "example-1"。如何使用 urllib 提取这些信息?我不想使用正则表达式为此。

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

from urllib.parse import urlparse

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

Urlparse returns 已解析的对象。我们可以使用这个解析器对象的路径,用“/”分割。这是结果:

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