简体   繁体   中英

Remove newlines in beautiful soup

In BeautifulSoup, I have the following:

>>> tr = soup.find_all('tr')[1]
<tr>
<td>Adaptive Systems Seminar (HOC+WPO)</td>
<td>wo</td>
<td>13:00</td>
<td>17:00</td>
<td>4:00</td>
<td>22-29, 32-36</td>
<td>MANDERICK BERNARD</td>
<td> </td>
</tr>

However, I'm just interested in the text. So I do

>>> tr(text=True)
[u'\n', u'Adaptive Systems Seminar (HOC+WPO)', u'\n', u'wo', u'\n', u'13:00', u'\n', u'17:00', u'\n', u'4:00', u'\n', u'22-29, 32-36', u'\n', u'MANDERICK BERNARD', u'\n', u'\xa0', u'\n']

I'd like to get the list above, but without all the newlines . I've read the documentation but I can't find anything about it.

One option would be to find all td elements inside and use get_text() :

In [4]: [td.get_text(strip=True) for td in soup.select("tr > td")]
Out[4]: 
[u'Adaptive Systems Seminar (HOC+WPO)',
 u'wo',
 u'13:00',
 u'17:00',
 u'4:00',
 u'22-29, 32-36',
 u'MANDERICK BERNARD',
 u'']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM