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://170.187.134.184'
print("searched url: ", find(URL))

In the above code, the string is scanning from left to right and the matches are returning in order.

OUTPUT:

 

searched url: ['http://170.187.134.184']

using string slicing method

here the URL ‘<a href=”http://170.187.134.184″>’ 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://170.187.134.184">'
url = html_string[9:-2:1]
print(html_string)
print(url)

The extracted URL from the string is,

OUTPUT:-

<a href="http://170.187.134.184">
http://170.187.134.184

Leave a Reply

Your email address will not be published. Required fields are marked *