vendredi 18 décembre 2020

Error in retrieving the HTML table values using Python if-else loop

Rewriting the question since the previous one was unclear. I am trying to retrieve elements from an HTML page using BeautifulSoup. Below is my HTML page snippet

<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>

I need the pass,fail and block status along with their corresponding Comments ,stored in three different lists. My output should look like this:

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

But I am getting this:

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

My Code:

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)

Only the third else condition is displayed as the output. Meaning even if there are pass and fail status , all are getting stored in blocked(third else) when trying to display the list. Would be great help if solved.

Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire