Create multiple URLs using each path of a URL in Python
The code below is to split a URL path and then create several URLs using each path in Python:
from urllib.parse import urlparse
def create_multiple_urls_from_url_path(url):
results = []
base_url = f"{urlparse(url).scheme}://{urlparse(url).netloc}"
path = urlparse(url).path
dirs_position = [pos for pos, char in enumerate(path) if char == "/"]
for i in dirs_position:
results.append(base_url+path[0:i+1])
return results
print(create_multiple_urls_from_url_path("https://www.example/home/dashboard/profile/index.php"))
The above code will give the following output:
['https://www.example/', 'https://www.example/home/', 'https://www.example/home/dashboard/', 'https://www.example/home/dashboard/profile/']