Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

808
Views
Python - Beautifulsoup - looping divs tags with same class names

I'm wondering how to scrap information off a website where there is multiple elements that have the same identifiers from which I want to scrap price data from. The issue I'm having is that when I loop through each div and print() I see its pasted multiple times in the console. I assume this is du to the div I'm locating encapsulated multiple elements with the same tag + classname.

HTML Page notation

GraphicPrice = soup.findAll('div', class_='col')

for price in GraphicPrice:
    prices = price.find('span', class_='price__amount')
    if prices is None:
        pass
    else:
        print(prices.text)

Output:

£859.99
£859.99
£1,049.99
£1,049.99
£829.99
£829.99
£899.99
£899.99
£999.95
£999.95
£999.95
£999.95

What I want is to eliminate the duplicate information and understand how to refactor my code to prevent this from happening.

I want every elements on the page prices to be printed. Currently I get the prices but it's duplicated 3 times.

Any help would be appreciated. (I'm still leaning) :)

about 4 years ago · Santiago Gelvez
3 answers
Answer question

0

first, you scrap all div tags with "col" class

soup.findAll('div', class_='col')

this div tag has one span class and that one class has two other nested span tags.

so, if you code like this

price.find('span', class_='price__amount') 

it scraps two span tags with "price_amount" on each div tag. that represents a wrong class.

if you want a second span tag then your code is like this.

soup.findAll('span', class_='price--sale--colored').find('span', class_='price__amount')
about 4 years ago · Santiago Gelvez Report

0

Use .select('span') to get object from inner of the selected object like

GraphicPrice = soup.findAll('div', class_='col')

for price in GraphicPrice:
    prices = price.select('span')
about 4 years ago · Santiago Gelvez Report

0

I stumbled upon such an error recently which brought me here looking for answers but alas, I figured it out and here I am posting the solution for those who'll be needing it in days to come

Mind you, there a different approaches to a problem

So, scrape the list like that but configure the list to output alternating elements of the list.

GraphicPrice = soup.findAll('div', class_='col')

for price in GraphicPrice:
    prices = price.find('span', class_='price__amount')
    if prices is None:
        pass
    else:
        print(prices.text)

### then loop the list to get ###alternating values

new_price= prices[1::2]
#or 
newprice= [prices[p] for p in range(len(prices)) if p%2 ==0]
about 4 years ago · Oloyede Abdul Ganiyu Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!