DevinGP
DevinGP

Reputation: 199

How to use BeautifulSoup to find an href link with a class

<div data-pet-card="pet-card" class="pet-card">

    <a data-pet-card="pet-card-link" href="https://Link-I-Want.com" 
    class="pet-card__link">

I am used to scraping html with BS4 but I am not super familiar with html itself and haven't come across an href that also has a class and the data-pet-card="pet-card-link" thing. I tried:

for a in soup.find_all('a', href=True):
    print("Found the URL:", a['href'])

but it prints nothing, and gives no errors.

Anything is helpful, thank you.

Upvotes: 5

Views: 11390

Answers (2)

Daniel Roseman
Daniel Roseman

Reputation: 599610

The attribute you put in the find_all call is the thing you have, not the thing you want to find. Here you have the class, so use that:

for a in soup.find_all('a', class_="pet-card__link"):
    print("Found the URL:", a['href']) 

(Because class is a reserved word in Python, you need to use class_ here.)

Upvotes: 6

yogkm
yogkm

Reputation: 649

for a in soup.find_all('a', href=True):
    print("Found the URL:", a.get_attribute_list('href')[0])

Please try this solution.

Upvotes: 0

Related Questions