【问题标题】:Error in retrieving the HTML table values using Python if-else loop使用 Python if-else 循环检索 HTML 表值时出错
【发布时间】:2020-12-18 22:25:24
【问题描述】:

重写问题,因为上一个问题不清楚。我正在尝试使用 BeautifulSoup 从 HTML 页面中检索元素。下面是我的 HTML 页面 sn-p

<span class="cars">Imported</span><br>
<span class="auto"> Cycle 1 / Step 1</span>
<hr noshade><BR>
<table class="table" width="100%" border="0">
     <colgroup>
        <col width="200">
        <col>
     </colgroup>
<tr><td>Macro:</td><td align="left">abc</td></tr>
<tr><td>Comment: </td><td align="left">Valid</td></tr>
<tr><td>status:</td><td align="left" class="prog_stat_pass">PASS</td></tr>
</table></td></tr>

span class="cars">Exported</span><br>
<span class="manual"> Cycle 1 / Step 26</span>
<hr noshade><BR>
<table class="table" width="100%" border="0">
     <colgroup>
        <col width="200">
        <col>
     </colgroup>
<tr><td>Macro:</td><td align="left">def</td></tr>
<tr><td>Comment: </td><td align="left">Valid</td></tr>
<tr><td>status:</td><td align="left" class="prog_stat_blocked">BLOCKED</td></tr>
</table></td></tr>

span class="cars">Transferred</span><br>
<span class="manual"> Cycle 1 / Step 26</span>
<hr noshade><BR>
<table class="table" width="100%" border="0">
     <colgroup>
        <col width="200">
        <col>
     </colgroup>
<tr><td>Macro:</td><td align="left">efg</td></tr>
<tr><td>Comment: </td><td align="left">Invalid</td></tr>
<tr><td>status:</td><td align="left" class="prog_stat_fail">Failed</td></tr>
</table></td></tr>

我需要存储在三个不同列表中的通过、失败和阻止状态以及它们相应的评论。我的输出应该是这样的:

PASS ['Valid']
FAIL ['Invalid']
BLOCKED ['Valid']

但我得到了这个:

PASS []
FAIL []
BLOCKED [['Valid,Valid,Invalid']]

我的代码:

self.table = self.soup_file.findAll(class_="table")


self.Macro = [column.findAll('td')[1].get_text() for column in self.table]
self.Comment = [column.findAll('td')[3].get_text() for column in self.table]
self.status = [column.findAll('td')[5].get_text() for column in self.table]
if self.status == "PASS":
    self.pass_.append(self.Comment)
elif self.status == "FAIL":
    self.fail_.append(self.Comment)
else:
    self.blocked_.append(self.Comment)

只有第三个 else 条件显示为输出。这意味着即使有通过和失败状态,当试图显示列表时,所有的都被存储在 blocked(third else) 中。如果解决了会很有帮助。

提前致谢。

【问题讨论】:

    标签: python html python-3.x if-statement beautifulsoup


    【解决方案1】:

    您可以执行以下操作:

    • 遍历表
    • 将每个表数据放入字典中
    • 在结果字典中将所有内容组合在一起,其中键是状态值

    类似的东西:

    In [3]: from collections import defaultdict
    
    In [4]: results = defaultdict(list)
    
    In [5]: for table in soup.select('table.table'):
                table_data = {row.td.get_text(strip=True).rstrip(':'): row('td')[-1].get_text(strip=True)  
                              for row in table('tr')}
                results[table_data['status']].append(table_data)
    
    In [6]: dict(results)
    Out[6]: 
    {'PASS': [{'Macro': 'abc', 'Comment': 'Valid', 'status': 'PASS'}],
     'BLOCKED': [{'Macro': 'def', 'Comment': 'Valid', 'status': 'BLOCKED'}],
     'Failed': [{'Macro': 'efg', 'Comment': 'Invalid', 'status': 'Failed'}]}
    

    【讨论】:

    • 感谢您的建议。但我只想要列表中状态的相应评论,因为我已经能够检索整个列表。不幸的是,当尝试仅检索相应状态的评论时,它会给出关键错误。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-08-27
    • 2016-10-23
    • 2021-08-02
    • 1970-01-01
    • 1970-01-01
    • 2023-03-27
    • 2014-06-07
    相关资源
    最近更新 更多