code-8
code-8

Reputation: 58730

How can I print all links within a found elements?

I'm new to BeautifulSoup, I found all the cards, about 12. But when I'm trying to loop through each card and print link href. I kept getting this error

AttributeError: ResultSet object has no attribute 'find'. You're probably treating a list of elements like a single element. Did you call find_all() when you meant to call find()?

from bs4 import BeautifulSoup

with open("index.html") as fp:
    soup = BeautifulSoup(fp, 'html.parser')

cards = soup.find_all('div', attrs={'class': 'up-card-section'})

# print(cards)
print(len(cards))

for link in cards.find_all('a'):
     print(link.get('href'))

Upvotes: 0

Views: 72

Answers (1)

0stone0
0stone0

Reputation: 44102

cards = soup.find_all('div', attrs={'class': 'up-card-section'})

Will return a collection of all the div's found, you'll need to loop over them before finding the chil a's.

That said, you should probably use findChildren for finding the a elements.


Example Demo with an minimal piece of HTML

from bs4 import BeautifulSoup


html = """
<div class='up-card-section'>
    <div class='foo'>
        <a href='example.com'>FooBar</a>
    </div>
</div>
<div class='up-card-section'>
    <div class='foo'>
        <a href='example2.com'>FooBar</a>
    </div>
</div>
"""

res = []
soup = BeautifulSoup(html, 'html.parser')

for card in soup.findAll('div', attrs={'class': 'up-card-section'}):
    for link in card.findChildren('a', recursive=True):
        print(link.get('href'))

Output:

example.com
example2.com

Upvotes: 1

Related Questions