Beautiful Soup [Python] and extracting text into a table

I'm new to Python and the Beatiful Soup! I heard about BS. They say that this is a great tool for analyzing and extracting content. So here I am ...:

I want to take the contents of the first td table into an html document. For example, I have this table

<table class="bp_ergebnis_tab_info">
    <tr>
            <td>
                     This is a sample text
            </td>

            <td>
                     This is the second sample text
            </td>
    </tr>
</table>

How can I use beautifulsoup to take the text “This is a sample text”? I use soup.findAll ('table', attrs = {'class': 'bp_ergebnis_tab_info'}) to get the whole table.

Thanks ... or should I try to get all the material with Perl ... that I am not familiar with. Another soltion will be a regular expression in PHP.

See target [1]: http://www.schulministerium.nrw.de/BP/SchuleSuchen?action=799.601437941842&SchulAdresseMapDO=142323

; html - , . PHP-, PHP. Perl .

+5
2

( ). find findall ( , ), [0], ):

table = soup.find('table' ,attrs={'class':'bp_ergebnis_tab_info'})

find, td:

first_td = table.find('td')

renderContents() :

text = first_td.renderContents()

... ( strip() :

trimmed_text = text.strip()

:

>>> print trimmed_text
This is a sample text
>>>

.

+12

Beautiful Soup , :-) , , . BeautifulSoup(html).prettify(), .

, soup.findAll(...) Beautiful Soup, , :

table_soup = soup.findAll('table' ,attrs={'class':'bp_ergebnis_tab_info'})
your_sample_text = table_soup.find("td").renderContents().strip()

print your_sample_text
-1

All Articles