Home >Backend Development >Python Tutorial >How to Extract Specific Cells from a Table Using Python\'s BeautifulSoup?

How to Extract Specific Cells from a Table Using Python\'s BeautifulSoup?

DDD
DDDOriginal
2024-10-29 09:08:02269browse

How to Extract Specific Cells from a Table Using Python's BeautifulSoup?

Python BeautifulSoup Parsing Table

Question:

I'm using Python's BeautifulSoup to parse a table containing parking ticket information, but I'm struggling to access specific cells within the table. Can anyone assist me in extracting all the rows and their corresponding cells?

Relevant Code:

<code class="python">soup = BeautifulSoup(plateRequest.text)
table = soup.find("table", {"class": "lineItemsTable"})
for row in table.findAll("tr"):
    cells = row.findAll("td")
    print(cells)</code>

Solution:

To efficiently access rows and cells, use the following modified code:

<code class="python">data = []
table = soup.find('table', attrs={'class':'lineItemsTable'})
table_body = table.find('tbody')

rows = table_body.find_all('tr')
for row in rows:
    cols = row.find_all('td')
    cols = [ele.text.strip() for ele in cols]
    data.append([ele for ele in cols if ele]) # Remove empty values</code>

Output:

Running the revised code will provide a list of lists, where each inner list represents a row in the table, and each element within the inner list is the text content of a cell:

[
    ['1359711259', 'SRF', '08/05/2013', '5310 4 AVE', 'K', '19', '125.00', '$'], 
    ['7086775850', 'PAS', '12/14/2013', '3908 6th Ave', 'K', '40', '125.00', '$'], 
    ['7355010165', 'OMT', '12/14/2013', '3908 6th Ave', 'K', '40', '145.00', '$'], 
    ['4002488755', 'OMT', '02/12/2014', 'NB 1ST AVE @ E 23RD ST', '5', '115.00', '$'], 
    ['7913806837', 'OMT', '03/03/2014', '5015 4th Ave', 'K', '46', '115.00', '$'], 
    ['5080015366', 'OMT', '03/10/2014', 'EB 65TH ST @ 16TH AV E', '7', '50.00', '$'], 
    ['7208770670', 'OMT', '04/08/2014', '333 15th St', 'K', '70', '65.00', '$'], 
    ['.00\n\n\nPayment Amount:']
]

Notes:

  • The last row in the output is the payment amount, which is not part of the table.
  • The last column in each row represents an input text box and may require special handling.

The above is the detailed content of How to Extract Specific Cells from a Table Using Python\'s BeautifulSoup?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn