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]|[[email protected]&+]|[!*\(\), ]|(?:%[0-9a-fA-F][0-9a-fA-F]))+',URL) return url URL ='I am a blogger at http://220.127.116.11' 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: ['http://18.104.22.168']
using string slicing method
here the URL ‘<a href=”http://22.214.171.124″>’ 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://126.96.36.199">' url = html_string[9:-2:1] print(html_string) print(url)
The extracted URL from the string is,
<a href="http://188.8.131.52"> http://184.108.40.206
Leave a Reply