从 table 行中获取带有漂亮汤的文本
Getting text from table row with beautiful soup
我想用漂亮的汤从标签之间提取文本。到目前为止我有:
def table_to_text(html):
from bs4 import BeautifulSoup
soup = BeautifulSoup(html)
trs = soup.findAll('tr')
for tr in trs:
print 'row '
print tr.findAll(['td','th']).text
这给我的输出看起来像:
row
[<td> AAA </td>, <td>Chi</td>, <td></td>, <td class="center"><span class="blue">1353</span>/<span class="red">23</span></td>]/n
我想让输出看起来像:
[ AAA , Chi, , 1353, 23]
我该怎么做?
.findAll
returns 一个列表,所以你需要另一个这样的 for 循环:
[el.text for el in sp.find_all(['td', 'th']) if el.text]
我想用漂亮的汤从标签之间提取文本。到目前为止我有:
def table_to_text(html):
from bs4 import BeautifulSoup
soup = BeautifulSoup(html)
trs = soup.findAll('tr')
for tr in trs:
print 'row '
print tr.findAll(['td','th']).text
这给我的输出看起来像:
row
[<td> AAA </td>, <td>Chi</td>, <td></td>, <td class="center"><span class="blue">1353</span>/<span class="red">23</span></td>]/n
我想让输出看起来像:
[ AAA , Chi, , 1353, 23]
我该怎么做?
.findAll
returns 一个列表,所以你需要另一个这样的 for 循环:
[el.text for el in sp.find_all(['td', 'th']) if el.text]