Extract URL from a string in Python
In this tutorial, we are going to learn how to extract URL from a string in Python. We are going to enter any string and check whether the string has any URL or not. If they had any URL then print the URL. This kind of problem can be solved by using certain methods. the methods are:-
using a regular expression
We will use the concept of Regular Expression in Python to solve the problem.
import re def find(URL): url = re.findall('http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\), ]|(?:%[0-9a-fA-F][0-9a-fA-F]))+',URL) return url URL ='I am a blogger at https://Codespeedy.com' print("searched url: ", find(URL))
In the above code, the string is scanning from left to right and the matches are returning in order.
searched url: ['https://Codespeedy.com']
using string slicing method
here the URL ‘<a href=”http://Codespeedy.com”>’ found in a string. the starting character found at the 9th index and the last char found at the -2(negative indexing)position. So that we can be using string slice we can extract the URL from a string.
html_string = '<a href="http://Codespeedy.com">' url = html_string[9:-2:1] print(html_string) print(url)
The extracted URL from the string is,
<a href="http://Codespeedy.com"> http://Codespeedy.com