【问题标题】:Arranging a table in Excel as it's displayed in HTML with Python and bs4使用 Python 和 bs4 在 Excel 中排列表格,因为它以 HTML 格式显示
【发布时间】:2020-08-21 07:19:25
【问题描述】:

我已经成功抓取了一张我想在 .xlsx 中显示的表格

当它显示在浏览器中时,这就是我希望它在 excel 中显示的方式。

它应该显示的方式是

A1 = 1。

B1 = 准备协助与事件响应相关的行动和活动

C1 = 1.1

D1 = 确定事件响应的责任人和 WHS 立法要求

A2 = 空白

B2 = 空白

C2 = 1.2

D2 = 确定有关事件响应计划和报告的工作场所政策、程序和流程

下面是我的代码,后面是我抓取的 HTML。

for i in Elements.findAll('tr'):
    columns = i.findAll('td')
    output_row = []
    for column in columns:
        sub_rows = column.findAll('p')
        for row in sub_rows:
            output_row.append(row.get_text(separator=' '))
    Element_rows.append(output_row)

-----------------------------------------------------------------

<table class="ait-table" width="943">
<tr>
<td style="border:1px solid ;;vertical-align: top;" width="299">
<p class="ait4"><strong class="ait24">ELEMENTS</strong>�</p>
</td>
<td style="border:1px solid ;;vertical-align: top;" width="766">
<p class="ait4"><strong class="ait24">PERFORMANCE CRITERIA</strong>�</p>
</td>
</tr>
<tr>
<td style="border:1px solid ;;vertical-align: top;" width="299">
<p class="ait4"><em class="ait7">Elements describe the essential outcomes.</em></p>
</td>
<td style="border:1px solid ;;vertical-align: top;" width="766">
<p class="ait4"><em class="ait7">Performance criteria describe the performance needed to demonstrate achievement of the element.</em></p>
</td>
</tr>
<tr>
<td style="border:1px solid #333333;;vertical-align: top;" width="299">
<p class="ait4">1. Prepare to assist with actions and activities associated with incident response</p>
</td>
<td style="border:1px solid #333333;;vertical-align: top;" width="766">
<p class="ait4">1.1 Identify duty holders and WHS legislative requirements for incident response</p>
<p class="ait4">1.2 Identify workplace policies, procedures and processes concerning incident response planning and reporting</p>
<p class="ait4">1.3 Communicate requirements for responding to incident to required personnel within scope of own role and work area</p>
<p class="ait4">1.4 Contribute to developing communication mechanisms to notify manager of incident</p>
</td>
</tr>
<tr>
<td style="border:1px solid #333333;;vertical-align: top;" width="299">
<p class="ait4">2. Assist with implementing response procedures during incident</p>
</td>
<td style="border:1px solid #333333;;vertical-align: top;" width="766">
<p class="ait4">2.1 Provide initial assistance to those involved in incident within scope of own role and expertise and according to organisational incident response policies and procedures</p>
<p class="ait4">2.2 Assist with documenting incident according to workplace procedures and processes</p>
<p class="ait4">2.3 Assist with meeting legislative requirements regarding incident, within scope of own role and expertise</p>
<p class="ait4">2.4 Assist with reporting incident to external authorities, according to legislative requirements and workplace procedures and processes </p>
</td>
</tr>
<tr>
<td style="border:1px solid #333333;;vertical-align: top;" width="299">
<p class="ait4">3. Contribute to collecting WHS information about incident</p>
</td>
<td style="border:1px solid #333333;;vertical-align: top;" width="766">
<p class="ait4">3.1 Assist with obtaining information and data from those involved about actions and events leading up to, during and after an incident, using appropriate data collection techniques</p>
<p class="ait4">3.2 Assist with identifying and accessing sources of additional information and data related to incident</p>
<p class="ait4">3.3 Compile and enter information according to record-keeping requirements</p>
</td>
</tr>
<tr>
<td style="border:1px solid #333333;;vertical-align: top;" width="299">
<p class="ait4">4. Assist with incident investigation</p>
</td>
<td style="border:1px solid #333333;;vertical-align: top;" width="766">
<p class="ait4">4.1 Assist with applying required incident investigation processes</p>
<p class="ait4">4.2 Use appropriate analysis techniques to interpret causes of incident and communicate with advisors when participating in workplace investigations</p>
<p class="ait4">4.3 Review incident reports according to organisational policies and procedures</p>
<p class="ait4">4.4 Contact responsible persons and relevant authorities as outlined in WHS laws, and organisational policies and procedures</p>
<p class="ait4">4.5 Contribute to communicating investigation outcomes to relevant stakeholders according to organisational policies and procedures</p>
</td>
</tr>
<tr>
<td style="border:1px solid #333333;;vertical-align: top;" width="299">
<p class="ait4">5. Contribute to developing and implementing recommended measures and actions arising from incident investigation</p>
</td>
<td style="border:1px solid #333333;;vertical-align: top;" width="766">
<p class="ait4">5.1 Contribute to developing incident investigation recommendations </p>
<p class="ait4">5.2 Assist with obtaining approval of developed recommendations from required stakeholders according to organisational policies and procedures</p>
<p class="ait4">5.3 Assist with communicating approved recommendations to required stakeholders according to organisational policies and procedures</p>
<p class="ait4">5.4 Contribute to implementing recommended measures and actions arising from incident investigation within scope of own role and according to WHS legislative requirements</p>
</td>
</tr>
</table>

【问题讨论】:

  • 很抱歉造成混乱。

标签: python python-3.x csv html-table beautifulsoup


【解决方案1】:

此示例使用reitertools.zip_longest 获取所需值并使用csv 模块编写文件(html_data 是您问题中的代码 sn-p):

import re
import csv
from bs4 import BeautifulSoup
from itertools import zip_longest

soup = BeautifulSoup(html_data, 'html.parser')
tds = soup.select('td')

with open('data.csv', 'w', newline='') as csvfile:
    writer = csv.writer(csvfile, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)

    for td1, td2 in zip(tds[::2], tds[1::2]):
        cell_ab = re.findall(r'(\d+.)\s*(.*)', td1.text)
        if not cell_ab:
            continue
        cell_cd = re.findall(r'(\d+.\d+)\s*(.*)', td2.text)

        for (a, b), (c, d) in zip_longest(cell_ab, cell_cd, fillvalue=(None, None)):
            writer.writerow([a, b, c, d])

结果是文件data.csv(来自我的 LibreOffice Calc 的屏幕截图):

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-09-20
    • 2021-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-02
    相关资源
    最近更新 更多