Oguz
Oguz

Reputation: 35

get deeper values from table with BeautifulSoup Python

Hi guys ı would like to get price values in this table i tried many codes but ı couldn't get .

  <div class="row">
        <div class="col-xs-60 dephTableBuy">
            <table class="dephTableBuyTable">
                <thead>
                    <tr>
                        <td>M</td>
                        <td>F</td>
                    </tr>
                </thead>
                <tbody id="bidOrders-temp-inj">
                            <tr class="orderRow" amount="1.3" price="9000" total="12000">
                                <td class="amount forBuyFirstTH">1.34742743</td>
                                <td class="price forBuyLastTH"><span class='part1'>9290</span>.<span class='part2'>00</span> <span class="buyCircleAdv"></span></td>
                            </tr>
                            <tr class="orderRow" amount="0.2" price="9252.02" total="2466.10">
                                <td class="amount forBuyFirstTH">0.2</td>
                                <td class="price forBuyLastTH"><span class='part1'>9,252</span>.<span class='part2'>02</span> <span class="buyCircleAdv"></span></td>
                            </tr>

Here is my useless code :

table = soup.find_all("table",{"class": "dephTableBuyTable"})

for item in table:
    print(item.contents[0].find_all("tr",{"class": "price"})[0].text)

ty

Upvotes: 0

Views: 202

Answers (1)

akuiper
akuiper

Reputation: 215047

There are two places that contain price in each tr tag: the price attribute and the second td cell:

Locate the rows:

tr = soup.find('table', {'class': 'dephTableBuyTable'}).find_all('tr', {'class': 'orderRow'})

To get the price in the tag attribute, just use row['price']:

for row in tr:
    print(row['price'])

# 9000
# 9252.02

To get the price in the td tag, you can use find to get the td cell and then use text attribute:

for row in tr:
    print(row.find('td', {'class': 'price'}).text)

# 9290.00 
# 9,252.02 

Upvotes: 2

Related Questions