Python-courses-download-Python-courses-s-Python-courses

Exercise 66

Write a Python program that extract the list of all urls from given a string s. Example if s = "You can use google https://www.google.com or facebook https://www.facebook.com" The algorithm returns the list

['https://www.google.com' , 'https://www.facebook.com']

Solution

def extractURL(s):
# initializing the list of urls:
listURL = []

# converting the string s into a list
L = s.split()

for element in L:
if (element.startswith('https://') or element.startswith('http://')):
listURL.append(element)
return listURL

# Teting algorithm
s = "You can use google http://www.google.com or facebook https://www.facebook.com"
print(extractURL(s)) # The output is:
# ['http://www.google.com', 'https://www.facebook.com']

Younes Derfoufi
my-courses.net

Leave a Reply