Last modified: Aug 29, 2023 By Alexander Williams
How to Get href of Element using BeautifulSoup [Easily]
To get the href attribute of <a> tag, use the following syntax:
tag.get('href')
The get('href')
method returns the value of the href attribute of the anchor tag.
By using the above syntax, we'll learn how to:
- Get the href attribute of a tag
- Get the href attribute of all tags
Get the href attribute of a tag
In the following example, we'll see how to use get('href'):
from bs4 import BeautifulSoup
# HTML content
html = '''
<a href="/python/string">Python string</a>
'''
# Create a BeautifulSoup object
soup = BeautifulSoup(html, 'html.parser')
# Find the 'a' tag
link_tag = soup.find('a')
# Extract the 'href' attribute value
href_value = link_tag.get('href')
print("HREF:", href_value)
Output:
HREF: /python/string
In the above example, we use the find()
method to find the first <a> tag. Then, we use the .get()
method on the link_tag object to get the href attribute's value.
Get the href attribute of all tags
You can use the find_all()
method to find all of the anchor tags and iterate through the results to get the href attribute of each anchor tag.
Here is an example:
from bs4 import BeautifulSoup
# HTML content
html = '''
<a href="/python/string">Python string</a>
<a href="/javascript/array">JavaScript array</a>
'''
# Create a BeautifulSoup object
soup = BeautifulSoup(html, 'html.parser')
# Find all anchor tags
link_tags = soup.find_all('a')
# Loop through each anchor tag
for tag in link_tags:
# Get the value of the 'href' attribute
href = tag.get('href')
# Print the 'href' attribute value
print(href)
Output:
/python/string
/javascript/array
Conclusion
In the end, we learned how to get the href value of a <a> tag by using the get('href')
with find()
methods.
We've also learned how to use the get('href')
and find_all()
methods to get the href of all <a> tags.