Sometimes, we want to get href with Python BeautifulSoup.
In this article, we’ll look at how to get href with Python BeautifulSoup.
How to get href with Python BeautifulSoup?
To get href with Python BeautifulSoup, we can use the find_all
method.
For instance, we write
from BeautifulSoup import BeautifulSoup
html = '''<a href="some_url">next</a>
<span class="class"><a href="another_url">later</a></span>'''
soup = BeautifulSoup(html)
for a in soup.find_all('a', href=True):
print(a['href'])
to create soup
object with BeautifulSoup
class called with the html
string.
Then we find the a
elements with the href
attribute returned by calling find_all
with 'a'
and href
set to True
.
Then we print out the href attribute values of the a
elements in the loop.
Conclusion
To get href with Python BeautifulSoup, we can use the find_all
method.