-2

PYTHON-beautifulsoup:

after selecting: divtotals = soup.find(id="od-subtotals")

I get this mess with multiple same class. I want select the last span which has $6.48

<div id="od-subtotals" class="a-fixed-right-grid-col a-col-right" style="width:260px;margin-right:-260px;float:left;">
        
    <div class="a-row">
        <div class="a-column a-span7 a-text-left">
                <span class="a-color-base">
                    Item(s) Subtotal: 
                </span> 
        </div> 

        <div class="a-column a-span5 a-text-right a-span-last">
            <span class="a-color-base">
                $5.99
            </span> 
        </div> 
    </div> 

    
            
    <div class="a-row a-spacing-mini">
    </div>  
    <div class="a-row">
        <div class="a-column a-span7 a-text-left">
                <span class="a-color-base">
                    Total before tax:
                </span> 
        </div> 
        <div class="a-column a-span5 a-text-right a-span-last">
            <span class="a-color-base">
                $5.99
            </span> 
        </div> 
    </div> 
    

            
    <div class="a-row a-spacing-mini">
    </div>         
    <div class="a-row">
        <div class="a-column a-span7 a-text-left">
            <span class="a-color-base a-text-bold">
                Grand Total:
            </span> 
        </div> 
        <div class="a-column a-span5 a-text-right a-span-last">
            <span class="a-color-base a-text-bold">
                $6.48  <!-- return this value -->
            </span> 
        </div> 
    </div> 
    
</div>

sorry I am new to this, I am writing this cause stackoverflow won't let me post showing error It looks like your post is mostly code; please add some more details. I hope this is enough details

  • What so far have you tried from SO to get output simple HINT: you are finding div tag with id so mention everythin in `find` mehod like `tag-name` and `attrs` parameters – Bhavya Parikh May 29 '21 at 15:01
  • id= od-subtotals is the last unique identifier I found. I selected that but left with multiple divs and spans with same class. I want to select the last span and return text. – Joydeb Roy May 29 '21 at 15:08

1 Answers1

0
  1. I have taken your data as html so if data is not dynamic loaded and divs are visible you can try approach

  2. Where find all divs with a-column which return as list of 6 tag and last tag contains your information so use find with span methodd to get your output

from bs4 import BeautifulSoup
soup=BeautifulSoup(html,"html.parser")
soup.find_all("div",attrs={"class":"a-column"})[5].find("span").get_text(strip=True)

Output:

'$6.48'
Bhavya Parikh
  • 3,304
  • 2
  • 9
  • 19
  • when doing the final test I noticed the pages were dynamically loaded and had more divs inside od-subtotals div. I think if I select the next span after the span that contains Grand Total: text will give me right results everytime. Can you please show me how I can do it – Joydeb Roy May 29 '21 at 16:52
  • if possible can you provide url link so it will better for understanding ! – Bhavya Parikh May 29 '21 at 16:54
  • It requires login to see the pages. I don't think I can share the link. I edit my prev comment can you check if it makes sense – Joydeb Roy May 29 '21 at 16:55
  • Okay what i can suggest you try from this [post](https://stackoverflow.com/a/66878732/11583070) may be it will work! – Bhavya Parikh May 29 '21 at 17:00