We use affiliate links. They let us sustain ourselves at no cost to you.

How to get ‘href’ attribute of ‘a’ element using Beautiful Soup

A step-by-step guide on how to extract an URL from an a element.

Important: we will use a real-life example in this tutorial, so you will need requests and Beautifulsoup libraries installed.

Step 1. Let’s start by importing the Beautifulsoup library.

				
					from bs4 import BeautifulSoup
				
			

Step 2. Then, import the requests library.

				
					import requests
				
			

Step 3. Get a source code of your target landing page. We will use In-Depth Smartproxy Review & Performance Tests page in this example.

				
					r=requests.get("https://proxyway.com/reviews/smartproxy-proxies")
				
			

Universally applicable code would look like this:

				
					r=requests.get("Your URL")
				
			

Step 4. Convert HTML code into a Beautifulsoup object named soup.

				
					soup=BeautifulSoup(r.content,"html.parser")
				
			

Step 5. Then, find the href attribute you want to extract. We will be using this tag as an example:

Find button URL screenshot
				
					a_href=soup.find("a",{"class":"single-intro__cta"}).get("href")
				
			

Universally applicable code would look like this:

				
					a_href=soup.find("a",{"class":" class of your target a element"}).get("href")
				
			

Step 6. Let’s check if our code works by printing it out.

				
					print(a_href)
				
			

Results:

Congratulations, you’ve extracted an URL from an a element. Here’s the full script:

				
					from bs4 import BeautifulSoup
import requests
r=requests.get("https://proxyway.com/reviews/smartproxy-proxies")
soup=BeautifulSoup(r.content,"html.parser")
a_href=soup.find("a",{"class":"single-intro__cta"}).get("href")
print(a_href)