Split URL in Python
Question:
So, I have this URL: https://www.last.fm/music/Limp+Bizkit/Significant+Other
I want to split it, to only keep the Limp+Bizkit
and Significant+Other
part of the URL. These are variables, and can be different each time. These are needed to create a new URL (which I know how to do).
I want the Limp+Bizkit
and Significant+Other
to be two different variables. How do I do this?
Answers:
You can use the str.split
method and use the forward slash as the separator.
>>> url = "https://www.last.fm/music/Limp+Bizkit/Significant+Other"
>>> *_, a, b = url.split("/")
>>> a
'Limp+Bizkit'
>>> b
'Significant+Other'
You can use regular expressions to achieve this.
import regex as re
url = "https://www.last.fm/music/Limp+Bizkit/Significant+Other"
match = re.match("^.*//.*/.*/(.*)/(.*)", url)
print(match.group(1))
print(match.group(2))
You can replace https://www.last.fm/music/
in the URL to just get Limp+Bizkit/Significant+Other
. Then you can split it in half at the /
character to break it into two strings. Then the URL will be a list and you can access the indices with url[0]
and url[1]
:
>>> url = "https://www.last.fm/music/Limp+Bizkit/Significant+Other"
>>> url = url.replace("https://www.last.fm/music/",'').split('/')
>>> first_value = url[0]
>>> second_value = url[1]
>>> first_value
'Limp+Bizkit'
>>> second_value
'Significant+Other'
So, I have this URL: https://www.last.fm/music/Limp+Bizkit/Significant+Other
I want to split it, to only keep the Limp+Bizkit
and Significant+Other
part of the URL. These are variables, and can be different each time. These are needed to create a new URL (which I know how to do).
I want the Limp+Bizkit
and Significant+Other
to be two different variables. How do I do this?
You can use the str.split
method and use the forward slash as the separator.
>>> url = "https://www.last.fm/music/Limp+Bizkit/Significant+Other"
>>> *_, a, b = url.split("/")
>>> a
'Limp+Bizkit'
>>> b
'Significant+Other'
You can use regular expressions to achieve this.
import regex as re
url = "https://www.last.fm/music/Limp+Bizkit/Significant+Other"
match = re.match("^.*//.*/.*/(.*)/(.*)", url)
print(match.group(1))
print(match.group(2))
You can replace https://www.last.fm/music/
in the URL to just get Limp+Bizkit/Significant+Other
. Then you can split it in half at the /
character to break it into two strings. Then the URL will be a list and you can access the indices with url[0]
and url[1]
:
>>> url = "https://www.last.fm/music/Limp+Bizkit/Significant+Other"
>>> url = url.replace("https://www.last.fm/music/",'').split('/')
>>> first_value = url[0]
>>> second_value = url[1]
>>> first_value
'Limp+Bizkit'
>>> second_value
'Significant+Other'