Sid
Sid

Reputation: 61

Loop through multiple tags in Python BeautifulSoup

I am trying to loop through multiple tags in the HTML so I can print all the IDs. My code right now prints only first ID, how can I print the second, third, fourth and so on values.

soup = BeautifulSoup(r.content, "html.parser")

product_div = soup.find_all('div', {'class': 'valu '})
product_tag = product_div[0].find('a')
products = product_tag.attrs['val']
print products

Upvotes: 1

Views: 3150

Answers (1)

Rakesh
Rakesh

Reputation: 82755

This should help

soup = BeautifulSoup(r.content, "html.parser")

for product_div in soup.find_all('div', {'class': 'size '}):
    product_tag = product_div.find('a')
    if product_tag:
        print product_tag.attrs['id']

Upvotes: 2

Related Questions