【发布时间】:2017-02-12 03:53:36
【问题描述】:
我正在尝试使用pandas.read_html 来解析一些表,但我注意到我检索到的HTML 在每个tr 中都有嵌套类。
link here:数据实际上是以json格式存储的。所以我解析它以提取html code。
我已经大大缩短了HTML,但我希望我仍然能够理解我的意思和我想要实现的目标。
<div class='\"tab_content' id='\"tab-profitability\"' style='\"display:block;\"' tab_override="">
<table cellpadding='\"0\"' cellspacing='\"0\"' class='\"r_table1' print97="" style='\"border-top:none;\"' text2="">
<colgroup>
<col width='\"23%\"'></col>
<col span='\"11\"' width='\"7%\"'></col>
</colgroup>
<thead>
<tr>
<th align='\"left\"' class='\"str' id='\"pr-margins\"' scope='\"col\"' text2="">Margins % of Sales</th>
<th align='\"right\"' id='\"pr-Y0\"' scope='\"col\"'>2006-12</th>
<th align='\"right\"' id='\"pr-Y1\"' scope='\"col\"'>2007-12</th>
<th align='\"right\"' id='\"pr-Y2\"' scope='\"col\"'>2008-12</th>
</tr>
</thead>
<tbody>
<tr class='\"hr\"'>
<td colspan='\"12\"'></td>
</tr>
<tr>
<th class='\"row_lbl\"' id='\"i12\"' scope='\"row\"'>Revenue</th>
<td align='\"right\"' headers='\"pr-Y0' i12="" pr-margins="">100.00</td>
<td align='\"right\"' headers='\"pr-Y1' i12="" pr-margins="">100.00</td>
<td align='\"right\"' headers='\"pr-Y2' i12="" pr-margins="">100.00</td>
</tr>
<tr class='\"hr\"'>
<td colspan='\"12\"'></td>
</tr>
<tr>
<th class='\"row_lbl\"' id='\"i13\"' scope='\"row\"'>COGS</th>
<td align='\"right\"' headers='\"pr-Y0' i13="" pr-margins="">49.55</td>
<td align='\"right\"' headers='\"pr-Y1' i13="" pr-margins="">55.63</td>
<td align='\"right\"' headers='\"pr-Y2' i13="" pr-margins="">69.97</td>
</tr>
<tr class='\"hr\"'>
<td colspan='\"12\"'>
<div class='\"hspacer2\"'>
<table cellpadding='\"0\"' cellspacing='\"0\"' class='\"r_table1' print97="" style='\"border-top:none;\"' text2="">
<colgroup>
<col width='\"23%\"'></col>
<col span='\"11\"' width='\"7%\"'></col>
</colgroup>
<thead>
<tr>
<th align='\"left\"' class='\"str' id='\"pr-profit\"' scope='\"col\"' text2="">Profitability</th>
<th align='\"right\"' id='\"pr-pro-Y0\"' scope='\"col\"'>2006-12</th>
<th align='\"right\"' id='\"pr-pro-Y1\"' scope='\"col\"'>2007-12</th>
<th align='\"right\"' id='\"pr-pro-Y2\"' scope='\"col\"'>2008-12</th>
</tr>
</thead>
<tbody>
<tr class='\"hr\"'>
<td colspan='\"12\"'></td>
</tr>
<tr>
<th class='\"row_lbl\"' id='\"i21\"' scope='\"row\"'>Tax Rate %</th>
<td align='\"right\"' headers='\"pr-pro-Y0' i21="" pr-profit="">22.17</td>
<td align='\"right\"' headers='\"pr-pro-Y1' i21="" pr-profit="">5.29</td>
<td align='\"right\"' headers='\"pr-pro-Y2' i21="" pr-profit="">11.59</td>
</tr>
<tr class='\"hr\"'>
<td colspan='\"12\"'></td>
</tr>
<tr>
<th class='\"row_lbl\"' id='\"i22\"' scope='\"row\"'>Net Margin %</th>
<td align='\"right\"' headers='\"pr-pro-Y0' i22="" pr-profit="">13.06</td>
<td align='\"right\"' headers='\"pr-pro-Y1' i22="" pr-profit="">17.09</td>
<td align='\"right\"' headers='\"pr-pro-Y2' i22="" pr-profit="">10.65</td>
</tr>
<tr class='\"hr\"'>
<td colspan='\"12\"'>
<div class='\"tab_content' id='\"tab-growth\"' style='\"display:none;\"' tab_override="">
<table cellpadding='\"0\"' cellspacing='\"0\"' class='\"r_table1' print97="" style='\"border-top:none;\"' text2="">
<colgroup>
<col width='\"23%\"'></col>
<col span='\"11\"' width='\"7%\"'></col>
</colgroup>
<thead>
<tr>
<th></th>
<th align='\"right\"' id='\"gr-Y0\"' scope='\"col\"'>2006-12</th>
<th align='\"right\"' id='\"gr-Y1\"' scope='\"col\"'>2007-12</th>
<th align='\"right\"' id='\"gr-Y2\"' scope='\"col\"'>2008-12</th>
</tr>
</thead>
<tbody>
<tr class='\"hr\"'>
<td colspan='\"12\"'></td>
</tr>
<tr>
<th align='\"left\"' class='\"str' colspan='\"12\"' id='\"gr-revenue\"' scope='\"row\"' text2="">Revenue %</th>
</tr>
<tr class='\"hr\"'>
<td colspan='\"12\"'></td>
</tr>
<tr>
<th class='\"row_lbl\"' id='\"i28\"' scope='\"row\"'>Year over Year</th>
<td align='\"right\"' gr-revenue="" headers='\"gr-Y0' i28="">—</td>
<td align='\"right\"' gr-revenue="" headers='\"gr-Y1' i28="">48.48</td>
<td align='\"right\"' gr-revenue="" headers='\"gr-Y2' i28="">187.48</td>
</tr>
<tr class='\"hr\"'>
<td colspan='\"12\"'></td>
</tr>
<tr>
<th class='\"row_lbl\"' id='\"i29\"' scope='\"row\"'>3-Year Average</th>
<td align='\"right\"' gr-revenue="" headers='\"gr-Y0' i29="">—</td>
<td align='\"right\"' gr-revenue="" headers='\"gr-Y1' i29="">10.04</td>
<td align='\"right\"' gr-revenue="" headers='\"gr-Y2' i29="">61.51</td>
</tr>
如何打开 html 并将其解析为 pandas?
我确实注意到最后的每个tr 都有一个class: "r_table1" 名称。我已经尝试了下面的代码,看看我是否可以打开它,但它不起作用。
r = reqiest.get(r'url_link')
initial_html = bs4.BeautifulSoup(r.text, 'lxml')
for each_class in initial_html.findAll(attrs={'class': 'r_table1'}):
each_class.unwrap()
df = pandas.read_html(str(initial_html), flavor='lxml') # error message: lxml.etree.XMLSyntaxError: Unexpected end tag : col, line 1, column 886
【问题讨论】:
-
你能在某处上传一个真实的 HTML 页面(它可能会减少,但它应该是一个有效的 HTML)并在这里发布一个链接吗?
-
嗨@MaxU 我添加了链接和我所做的描述。谢谢你帮我解决这个问题。
html代码实际上在json文件中,所以我解析它以提取 html