Get href text of a link that has a specific class attribute using BeautifulSoup in Python

How to get only text from href in an anchor tag that matches the class. Therefore if i have

<a href="Link_I_Need.html" class="Unique_Class_Name">link text</a>

How can I get the Link_I_Need.html string only from the binding tag with the Unique_Class_Name class?

+4
source share
1 answer

Use .find()or .find_all()to select the element (s) that has an attribute hrefand a class attribute Unique_Class_Name. Then iterate over the elements and get access to the attribute value href:

soup = BeautifulSoup(html)
anchors = soup.find_all('a', {'class': 'Unique_Class_Name', 'href': True})

for anchor in anchors:
    print (anchor['href'])

CSS .select():

soup = BeautifulSoup(html)

for anchor in soup.select('a.Unique_Class_Name'):
    if anchor.has_attr('href'):
        print (anchor['href'])
+4

Source: https://habr.com/ru/post/1628889/


All Articles