以下是我要刮的内容(为了便于阅读,缩短了一吨):
<table class="sortable row_summable stats_table" id="per_game">
<colgroup><col><col><col><col><col><col><col><col><col><col><col><col><col><col><col><col><col><col><col><col><col><col><col><col><col><col><col><col><col></colgroup>
<thead>
<tr class="">
<th data-stat="season" align="center" class="tooltip sort_default_asc" tip="If listed as single number, the year the season ended.<br>★ - Indicates All-Star for league.<br>Only on regular season tables.">Season</th>
<th data-stat="age" align="center" class="tooltip sort_default_asc" tip="Age of Player at the start of February 1st of that season.">Age</th>
</tr>
</thead>
<tbody>
<tr class="full_table" id="per_game.2009">
<td align="left" ><a href="/players/r/rondora01/gamelog/2009/">2008-09</a></td>
<td align="right" >22</td>
</tr>
<tr class="full_table" id="per_game.2010">
<td align="left" ><a href="/players/r/rondora01/gamelog/2010/">2009-10</a><span class="bold_text" style="color:#c0c0c0"> ★</span></td>
<td align="right" >23</td>
</tr>
</tfoot>
</table>
下面是我使用的代码:
^{pr2}$但是,如果您在HTML中注意到,与第一行相比,第二行还有一个额外的span
。它创造了一颗小星星。我的代码运行直到有这个额外参数的任何行都会找到,然后崩溃。关于让代码足够灵活以忽略额外的span
块的想法?在
您可以通过以下方法改进代码:首先,将所有标题读入列表,并逐行读取所有参数,使用
zip()
将每个标题与值匹配,并生成字典:印刷品:
^{pr2}$如果要从参数值中去掉不可打印的字符,可以依赖^{} :
{另请参见^
输出到csv的完整代码(带播放器名):
我建议做一些改变:
由于您对与
<a>
元素相关联的文本感兴趣,请更改行:col[0].string
到col[0].a.string
。这样就能解决问题了。在第一个问题修复后,您将在该表的最后一行遇到一个错误(因为它的结构不同)。要解决这个问题,请将
for row in table.findAll('tr')[1:]:
更改为for row in table.findAll('tr')[1:-1]:
。这样可以跳过最后一行。进行上述更改:
印刷品:
^{pr2}$相关问题 更多 >
编程相关推荐