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をアンラップしてパンダに解析するにはどうすればよいですか?
最後に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
こんにちは@MaxU私はリンクと私が何をしたのかの説明を追加しました。これを手伝ってくれてありがとう。 'html'コードは実際に' json'ファイルの中にありますので、私はhtmlを抽出するためにそれを解析しました –