0

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

Lee Sai Mun
  • 140
  • 3
  • 13

1 Answers1

0
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']
Client
  • 140
  • 1
  • 6