Python BeautifulSoup 解析表
问题:
我正在使用 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中文网其他相关文章!