beautiful soup captures null values in a table

我只是一个虾纸丫 提交于 2019-12-13 05:53:18

问题


For the following piece of HTML code, I used beautifulsoup to capture the table information:

<table>
<tr>
<td><b>Code</b></td>
<td><b>Display</b></td>
</tr>
<tr>
<td>min</td>
<td>Minute</td><td/>
</tr>
<tr>
<td>happy </td>
<td>Hour</td><td/>
</tr>
<tr>
<td>daily </td>
<td>Day</td><td/>
</tr>

This is my code:

comments = [td.get_text()  for td in table.findAll("td")]
Comments=[data.encode('utf-8')  for data in comments] 

As you see, this table has two headers: "code and display" and some values in rows. The expected output of my code should be [code, display, min, minutes, happy, Hour, daily, day]

but this is the output:

['Code', 'Display', 'min', 'Minute', '', 'happy ', 
'Hour', '', 'daily ', 'Day', '']

The output has '' in 5th, 8th, and 11th indices in comments that are not defined in this table. I think it may because of </td><td/>. How can I change the code to not capture u'' in the output?


回答1:


Sorry, I hadn't read your question carefully enough. You're right, the problem is the empty <td/> tags. Just adjust your generator to only include cells with text:

comments = [td.get_text() for td in table.findAll('td') if td.text]


EDIT: I doubt this is the most efficient way to do it, but this will only include tds that have either text or a corresponding td in the first row.
ths = table.tr.find_all('td')
tds_in_row = len(table.tr.next_sibling.find_all('td'))

tds = [
    td.get_text()
    for i, td in enumerate(table.find_all('td'))
    if len(ths) > (i + 1) % tds_in_row or td.text
]


来源:https://stackoverflow.com/questions/37555709/beautiful-soup-captures-null-values-in-a-table

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!