简体   繁体   中英

How to skip HTML table row (tr) when one column (td) is empty in python 3

I am scraping data from an HTML table, once I have the HTML data using BeautifulSoup, I generate a list of floats from the columns. For my application, I want to compare the floats in pairs (eg list[0] with list[1], list[2] with list [3], etc). It works well, but then falls down when there is an empty cell, as my calculate then goes out of sync - eg list[n] is compares with list[n+2].

This is the code I use to generate the list of paired values (the values start as string fractions and I convert to floats):

amounts = table_body.find_all('td', attrs={'class': True}) 
amounts = [ele.text.strip('( )') for ele in amounts] 
amounts = [float(fractions.Fraction(x))+1 for x in amounts]
amountspairs = [odds[x:x+2] for x in range(0, len(amounts), 2)]

So my question is how do you obtain a value of say 0, when the cell is empty - the page I am scraping from just has this for an empty cell, whereas there are a number of attributes for cells that do have values - in code I just used
class=True

The HTML code for empty cell is < td > & nbsp; < /td >, so I'm trying to figure out how to get BeautifulSoup to return a value rather than ignore it, or if there is a way to skip the row if a cell is empty.

Many thanks (from a beginner)

l = []
for i in soup.find_all('td'):
    if i.text:
        l.append(float(i.text))
    else:
        l.append(float(0))

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