Python BeautifulSoup 解析表
問題:
問題:問題:
<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>
問題:
<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>
[ ['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:'] ]
解決方案:
要有效存取以下行和儲存格,請使用下列行修改後的程式碼:以上是如何使用 Python 的 BeautifulSoup 從表中提取特定單元格?的詳細內容。更多資訊請關注PHP中文網其他相關文章!