D
D
dar4q2020-12-08 21:37:53
Python
dar4q, 2020-12-08 21:37:53

How to parse the value of the desired td?

How to parse the "Manager's phone" value from the table if the table can have 2 or more lines with the same name, the number is not always at the very bottom of the table, and the number itself does not have a constant record format?

<div class="cnt"> 
                <div class="cnt-in visible"> 
                    <table> 
                        <tr> 
                            <td>Полное наименование:</td> 
                            <td><b>Открытое акционерное общество "Айсберг"</b></td> 
                        </tr> 
                         
                        <tr> 
                            <td>Краткое наименование:</td> 
                            <td><b>ОАО "Айсберг"</b></td> 
                        </tr>                             
                                                    
                        <tr> 
                            <td>Регион:</td> 
                            <td><b>Республика Башкортостан</b></td> 
                        </tr>             
                                     
                        <tr> 
                            <td>Часовой пояс:</td> 
                            <td><b>0</b></td> 
                        </tr>      
                        <tr> 
                            <td>Телефон руководителя:</td> 
                            <td><b>(347) 223-52-65</b></td> 
                        </tr>


So far, I myself have reached this point, I tried to determine the index of the cell that stores this value by the name "Telephone of the head" and take the next one, that is, with the number itself, but with a search in the list of problems. Thank you in advance
phone = soup.find('div', class_='cnt-in visible').find_all("td")

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
soremix, 2020-12-08
@dar4q

parent = soup.find('td', string='Телефон руководителя:')
number = parent.find_next('td').text

N
Nadim Zakirov, 2020-12-08
@zkrvndm

Your approach is fundamentally wrong. To search for numbers, it is best to stupidly sort through the cells in the forehead and check the text for compliance with a regular expression. A correctly compiled regular expression will pull out all the phones for you without any problems, even if they are recorded in different formats.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question