如何使用python从HTML文件读取数据并将数据写入CSV文件?

时间:2019-05-13 10:41:21

标签: python html pandas csv beautifulsoup

我有一个.html文件报告,其中包含有关表格和通过/失败标准的数据。所以我想使用Python3将这些数据写入.csv文件。 请建议我如何进行? 例如,数据将如下所示:

<h2>Sequence Evaluation of Entire Project &nbsp;&nbsp;&nbsp;<em class="contentlink"><a href="#contents">[Contents]</a></em> </h2>

<table width="100%" class="coverage">
  <tr class="nohover">
    <td colspan="8" class="tableabove">Test Sequence State</td>
  </tr>
  <tr>
    <th colspan="2" style="white-space:nowrap;">Metric</th>
    <th colspan="2">Percentage</th>
    <th>Target</th>
    <th>Total</th>
    <th>Reached</th>
    <th>Unreached</th>
  </tr>
  <tr>
    <td colspan="2">Test Sequence Work Progress</td>
    <td>100.0%</td>
    <td>
      <table class="metricbar">
        <tr class="borderX">
          <td class="white"></td>
          <td class="target"></td>
          <td class="white" colspan="2"></td>
        </tr>
        <tr>
          <td class="covreached" width="99%"></td>
          <td class="target" width="1%"></td>
          <td class="covreached" width="0%"></td>
          <td class="covnotreached" width="0%"></td>
        </tr>
        <tr class="borderX">
          <td class="white"></td>
          <td class="target"></td>
          <td class="white" colspan="2"></td>
        </tr>
      </table>
    </td>
    <td>100%</td>
    <td>24</td>
    <td>-</td>
    <td>-</td>
  </tr>
  <tr>

2 个答案:

答案 0 :(得分:0)

假设您知道标题,而实际上只需要相关的百分比,那么使用bs4 4.7.1可以使用:contains定位标题,然后使用下一个td。您将从文件中读取HTML到显示的html变量中。

import requests
from bs4 import BeautifulSoup as bs
import pandas as pd

html = '''
<h2>Sequence Evaluation of Entire Project &nbsp;&nbsp;&nbsp;<em class="contentlink"><a href="#contents">[Contents]</a></em> </h2>

<table width="100%" class="coverage">
  <tr class="nohover">
    <td colspan="8" class="tableabove">Test Sequence State</td>
  </tr>
  <tr>
    <th colspan="2" style="white-space:nowrap;">Metric</th>
    <th colspan="2">Percentage</th>
    <th>Target</th>
    <th>Total</th>
    <th>Reached</th>
    <th>Unreached</th>
  </tr>
  <tr>
    <td colspan="2">Test Sequence Work Progress</td>
    <td>100.0%</td>
    <td>
      <table class="metricbar">
        <tr class="borderX">
          <td class="white"></td>
          <td class="target"></td>
          <td class="white" colspan="2"></td>
        </tr>
        <tr>
          <td class="covreached" width="99%"></td>
          <td class="target" width="1%"></td>
          <td class="covreached" width="0%"></td>
          <td class="covnotreached" width="0%"></td>
        </tr>
        <tr class="borderX">
          <td class="white"></td>
          <td class="target"></td>
          <td class="white" colspan="2"></td>
        </tr>
      </table>
    </td>
    <td>100%</td>
    <td>24</td>
    <td>-</td>
    <td>-</td>
  </tr>
  <tr>
  '''
soup = bs(html, 'lxml') # 'html.parser' if lxml not installed
header = 'Test Sequence Work Progress'
result = soup.select_one('td:contains("' + header + '") + td').text
df = pd.DataFrame([result], columns = [header])
print(df)
df.to_csv(r'C:\Users\User\Desktop\data.csv', sep=',', encoding='utf-8-sig',index = False )

答案 1 :(得分:0)

import csv 
from bs4 import BeautifulSoup

out = open('out.csv', 'w', encoding='utf-8')
path="my.html"  #add the path of your local file here


soup = BeautifulSoup(open(path), 'html.parser')
for link in soup.find_all('p'): #add tag whichyou want to extract
 a=link.get_text()
 out.write(a)
out.write('\n')
out.close()