Python实现从HTML表格单元格提取src并导出至CSV文件
问题:提取HTML表格文本与图片文件名并导出CSV
现有如下HTML表格,需求是将表格中的文本或img标签src值中的文件名(如0.png、1.png)导出至CSV文件。
已编写的Python代码基于BeautifulSoup和Pandas,但仅能提取第三列(C列)的文本值,列A、B、D、E、F、G的内容无法正确获取(导出结果为空),需修改代码实现预期效果。
原代码
table1 = soup.find('table', id = 'pageLayout_projectTeamMembersGrdView_gridView') headers = [] for i in table1.find_all('th'): title = i.text.strip() headers.append(title) df = pd.DataFrame(columns = headers) for row in table1.find_all('tr')[1:]: data = row.find_all('td') row_data = [td.text.strip() for td in data] length = len(df) df.loc[length] = row_data df.to_csv('Export.csv', index=False) print("CSV created!")
HTML表格结构
<table class="GridView plm-table" id="pageLayout_projectTeamMembersGridView_gridView"> <tbody> <tr id="pageLayout_projectTeamMembersGridView_gridView_headerRow" class="GridViewHeaderRow"> <th class="GridViewHeader" scope="col">A</th> <th class="GridViewHeader" scope="col">B</th> <th class="GridViewHeader" scope="col">C</th> <th class="GridViewHeader" scope="col">D</th> <th class="GridViewHeader" scope="col">E</th> <th class="GridViewHeader" scope="col">F</th> <th class="GridViewHeader" scope="col">G</th> </tr> <tr id="pageLayout_projectTeamMembersGridView_DataRow0" class="GridViewRow"> <td class="GridViewCell" align="right"><input type="checkbox" name="ss" value="zz"></td> <td class="GridViewCell"><img class="Icon" src="../../Images/1.png" style="border-width:0px;"></td> <td class="GridViewCell">John</td> <td class="GridViewCell"><img id="Image0_IDcon" src="../../Images/0.png"></td> <td class="GridViewCell"><img id="Image1_IDcon" src="../../Images/1.png"></td> <td class="GridViewCell"><img id="Image1_IDcon" src="../../Images/1.png"></td> <td class="GridViewCell"><img id="Image0_IDcon" src="../../Images/0.png"></td> </tr> <tr id="pageLayout_projectTeamMembersGridView_DataRow1" class="GridViewRow"> <td class="GridViewCell" align="right"><input type="checkbox" name="ss" value="zz"></td> <td class="GridViewCell"><img class="Icon" src="../../Images/1.png" style="border-width:0px;"></td> <td class="GridViewCell">Steve</td> <td class="GridViewCell"><img id="Image1_IDcon" src="../../Images/1.png"></td> <td class="GridViewCell"><img id="Image1_IDcon" src="../../Images/1.png"></td> <td class="GridViewCell"><img id="Image0_IDcon" src="../../Images/0.png"></td> <td class="GridViewCell"><img id="Image0_IDcon" src="../../Images/0.png"></td> </tr> <tr id="pageLayout_projectTeamMembersGridView_DataRow2" class="GridViewRow"> <td class="GridViewCell" align="right"><input type="checkbox" name="ss" value="zz"></td> <td class="GridViewCell"><img class="Icon" src="../../Images/1.png" style="border-width:0px;"></td> <td class="GridViewCell">Mary</td> <td class="GridViewCell"><img id="Image0_IDcon" src="../../Images/0.png"></td> <td class="GridViewCell"><img id="Image1_IDcon" src="../../Images/1.png"></td> <td class="GridViewCell"><img id="Image1_IDcon" src="../../Images/1.png"></td> <td class="GridViewCell"><img id="Image0_IDcon" src="../../Images/0.png"></td> </tr> </tbody> </table>
修改后的代码
import os from bs4 import BeautifulSoup import pandas as pd # 假设soup已完成页面解析初始化 table1 = soup.find('table', id='pageLayout_projectTeamMembersGridView_gridView') # 生成表头 headers = [th.text.strip() for th in table1.find_all('th')] df = pd.DataFrame(columns=headers) # 遍历表格行(跳过表头行) for row in table1.find_all('tr')[1:]: data = row.find_all('td') row_data = [] for td in data: # 优先提取img标签的src文件名 img = td.find('img') if img: img_filename = os.path.basename(img['src']) row_data.append(img_filename) else: # 提取input标签的value值(针对A列复选框) input_tag = td.find('input') if input_tag: row_data.append(input_tag.get('value', '')) else: # 无特殊标签时提取文本 row_data.append(td.text.strip()) df.loc[len(df)] = row_data # 导出CSV df.to_csv('Export.csv', index=False) print("CSV created!")
关键修改说明
- 重构了行数据的生成逻辑,不再单纯提取td文本,而是针对不同标签类型做适配
- 对每个td元素依次判断:
- 存在img标签时,用
os.path.basename()提取src中的文件名 - 存在input标签时,提取其value属性值
- 其余情况提取td文本内容
- 存在img标签时,用
- 用列表推导式简化了表头生成代码
内容的提问来源于stack exchange,提问作者Herve
相关产品推荐
相关产品推荐

