繁体   English   中英

如何使用 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 返回已解析的 object。 我们可以使用这个解析器object的路径,用“/”分割。 结果如下:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM