首先,我不熟悉python和堆栈溢出,所以请友好一点。在
这是我要从中提取数据的html页面的源代码。在
网页:http://gbgfotboll.se/information/?scr=table&ftid=51168 这张表在这页的底部
<html>
table class="clCommonGrid" cellspacing="0">
<thead>
<tr>
<td colspan="3">Kommande matcher</td>
</tr>
<tr>
<th style="width:1%;">Tid</th>
<th style="width:69%;">Match</th>
<th style="width:30%;">Arena</th>
</tr>
</thead>
<tbody class="clGrid">
<tr class="clTrOdd">
<td nowrap="nowrap" class="no-line-through">
<span class="matchTid"><span>2014-09-26<!-- br ok --> 19:30</span></span>
</td>
<td><a href="?scr=result&fmid=2669197">Guldhedens IK - IF Warta</a></td>
<td><a href="?scr=venue&faid=847">Guldheden Södra 1 Konstgräs</a> </td>
</tr>
<tr class="clTrEven">
<td nowrap="nowrap" class="no-line-through">
<span class="matchTid"><span>2014-09-26<!-- br ok --> 13:00</span></span>
</td>
<td><a href="?scr=result&fmid=2669176">Romelanda UF - IK Virgo</a></td>
<td><a href="?scr=venue&faid=941">Romevi 1 Gräs</a> </td>
</tr>
<tr class="clTrOdd">
<td nowrap="nowrap" class="no-line-through">
<span class="matchTid"><span>2014-09-27<!-- br ok --> 13:00</span></span>
</td>
<td><a href="?scr=result&fmid=2669167">Kode IF - IK Kongahälla</a></td>
<td><a href="?scr=venue&faid=912">Kode IP 1 Gräs</a> </td>
</tr>
<tr class="clTrEven">
<td nowrap="nowrap" class="no-line-through">
<span class="matchTid"><span>2014-09-27<!-- br ok --> 14:00</span></span>
</td>
<td><a href="?scr=result&fmid=2669147">Floda BoIF - Partille IF FK </a></td>
<td><a href="?scr=venue&faid=218">Flodala IP 1</a> </td>
</tr>
</tbody>
</table>
</html>
我需要提取时间:19:30和团队名称:Guldhedens IK-IF Warta表示第一个和第二个表单元格(不是第三个),从第二个表格行提取13:00/Romelanda UF-IK Virgo等等。。所有的表格行都有。在
正如您可以看到的,每个表行在时间之前都有一个日期,所以这里有一个棘手的部分。我只想从那些日期等于我运行此代码的日期的表行中获取上面提到的时间和团队名称。在
到目前为止,我唯一能做的事情并不多,我只能使用以下代码获得时间和球队名称:
^{pr2}$这给了我结果['2014-09-26','19:30']在这之后,我不知道如何迭代不同的表行,希望找到与我运行代码的日期相匹配的特定表单元格。在
我希望你能尽可能多地回答。在
如果我能理解你,试试这样的方法:
我知道这是脆弱的,有更好的解决办法,但它是有效的。;)
编辑:
^{pr2}$使用BeautifulSoup的更好方法:
编辑2: 页面没有响应,但应该可以:
lxml公司:
所以多亏了@CodeNinja的帮助,我才稍微调整了一下,才得到我想要的东西。 我导入时间来获取运行代码的日期。不管怎样,这是我想要的代码。谢谢你的帮助!!在
所以这里是如何正确操作的最终版本。这将解析它拥有的所有表行,而不在for循环中使用“range”。我从我的另一个帖子得到了这个答案:Iterate through all the rows in a table using python lxml xpath
^{pr2}$相关问题 更多 >
编程相关推荐