Python Forum
How to retrieve value from td which under table/tbody/tr by python - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: How to retrieve value from td which under table/tbody/tr by python (/thread-27991.html)



How to retrieve value from td which under table/tbody/tr by python - DDNEW - Jun-30-2020

Output:
<table id="table-orders" cellpadding="0" cellspacing="0" border="0" class="mypage_table"> <colgroup> <col width="13%"> <col width="5%"> <col width="10%"> <col width="10%"> <col width="15%"> <col width="2%"> <col width="15%"> <col width="15%"> <col width="15%"> </colgroup> <thead> <tr style="cursor: default;"> <th colspan="2"><nobr>H1</nobr></th> <th><nobr>H2</nobr></th> <th><nobr>H3</nobr></th> <th colspan="3"><nobr>H4</nobr></th> <th><nobr>H5</nobr></th> <th><nobr>H6</nobr></th> </tr> </thead> <tbody> <tr data-idx="531006" data-no="AP86F561ED" data-type="AP"> <td><span class="btn btn-block badge" style="width: 85%; background-color: #CD0001; color: #FFFFFF;">B1</span></td> <td><nobr><img src="/images/common/ft_text.png" width="16" height="16">&nbsp;/&nbsp;<img src="/images/common/ft_file.png" width="16" height="16"></nobr></td> <td style="text-align:right;">0.5 HR</td> <td>B3</td> <td style="text-align:right;">B4</td> <td>B5</td> <td style="text-align:left;">B6</td> <td style="text-align:left;">B7</td> <td style="text-align:right;">B8</td> </tr> </tbody> </table>
   html = driver.page_source
    soup = BeautifulSoup(html,'html.parser')

    table = soup.find('table', id="table-orders")
    rows = table.findAll('tr')

    for tr in soup.find_all('table', id='table-orders'):
        tds = tr.find_all('td') 
        for td in tds:
            print(td.text)
            print(tds[2]) 
            print(tds[6])
I only need to get this one [ <td style="text-align:right;">0.5 HR</td>]
because I want to know if this value bigger than 5
can you teach me how to get it and replace HR ? ( my code return error Cry )
thank you very much


RE: How to retrieve value from td which under table/tbody/tr by python - Larz60+ - Jun-30-2020

Since you've made a good effort (this is untested code, so up to you to try and repair if necessary):
Also you will need to get the proper row. This code will peint all, with index
html = driver.page_source
soup = BeautifulSoup(html,'html.parser')
 
table = soup.find('tbody', {'data-no': 'AP86F561ED'})
tds = table.tr.find_all('td)
for n, td in enumerate(tds):
    print(f"index {n}: {td.text.strip()}")