从html文档中提取日期

时间:2015-07-08 05:31:44

标签: python beautifulsoup

我有一个非常大的html文件。 我需要抓取这个html文件并提取某些信息

soup.findAll('table',{"summary" : "This table displays snapshot information"})

[<table border="1" summary="This table displays snapshot information" width="500">
 <tbody><tr><th class="awrnobg" scope="col"></th><th class="awrbg" scope="col">Snap Id</th><th class="awrbg" scope="col">Snap Time</th><th class="awrbg" scope="col">Sessions</th><th class="awrbg" scope="col">Cursors/Session</th></tr>
 <tr><td class="awrnc" scope="row">Begin Snap:</td><td align="right" class="awrnc">98810</td><td align="center" class="awrnc">29-Jun-15 08:00:02</td><td align="right" class="awrnc">700</td><td align="right" class="awrnc">    129.6</td></tr>
 <tr><td class="awrc" scope="row">End Snap:</td><td align="right" class="awrc">98864</td><td align="center" class="awrc">29-Jun-15 17:00:23</td><td align="right" class="awrc">703</td><td align="right" class="awrc">    129.1</td></tr>
 <tr><td class="awrnc" scope="row">Elapsed:</td><td class="awrnc"> </td><td align="center" class="awrnc">             540.35 (mins)</td><td class="awrnc"> </td><td class="awrnc"> </td></tr>
 <tr><td class="awrc" scope="row">DB Time:</td><td class="awrc"> </td><td align="center" class="awrc">           2,963.17 (mins)</td><td class="awrc"> </td><td class="awrc"> </td></tr>
 </tbody></table>]

使用美丽的汤我设法得到一个清单。 但我需要提取日期29-Jun-15 08:00:02任何想法 我可以单独操作列表项,但这看起来很难看。

1 个答案:

答案 0 :(得分:5)

只需使用td搜索class即可。它应该返回一个列表,你可以从那里继续。

from bs4 import BeautifulSoup as bsoup

html = """<table border="1" summary="This table displays snapshot information" width="500">
 <tbody><tr><th class="awrnobg" scope="col"></th><th class="awrbg" scope="col">Snap Id</th><th class="awrbg" scope="col">Snap Time</th><th class="awrbg" scope="col">Sessions</th><th class="awrbg" scope="col">Cursors/Session</th></tr>
 <tr><td class="awrnc" scope="row">Begin Snap:</td><td align="right" class="awrnc">98810</td><td align="center" class="awrnc">29-Jun-15 08:00:02</td><td align="right" class="awrnc">700</td><td align="right" class="awrnc">    129.6</td></tr>
 <tr><td class="awrc" scope="row">End Snap:</td><td align="right" class="awrc">98864</td><td align="center" class="awrc">29-Jun-15 17:00:23</td><td align="right" class="awrc">703</td><td align="right" class="awrc">    129.1</td></tr>
 <tr><td class="awrnc" scope="row">Elapsed:</td><td class="awrnc"> </td><td align="center" class="awrnc">             540.35 (mins)</td><td class="awrnc"> </td><td class="awrnc"> </td></tr>
 <tr><td class="awrc" scope="row">DB Time:</td><td class="awrc"> </td><td align="center" class="awrc">           2,963.17 (mins)</td><td class="awrc"> </td><td class="awrc"> </td></tr>
 </tbody></table>"""

soup = bsoup(html)
print soup.find_all('td', class_='awrnc')[2].get_text()
# 29-Jun-15 08:00:02

编辑:

考虑到返回表列表的原始代码,只需使用普通列表索引/切片来获取所需的表。请参阅以下示例。我将上面的HTML更改为具有三个具有相同table属性的summary。我的代码将返回所有三个,所以我将选择第一个。然后,我将查找与我定义的td匹配的所有class。然后,我将使用[2]选择第三个。然后,我将使用get_text()获取目标td元素中的值。

from bs4 import BeautifulSoup as bsoup

html = """<html><body><table border="1" summary="This table displays snapshot information" width="500">
 <tbody><tr><th class="awrnobg" scope="col"></th><th class="awrbg" scope="col">Snap Id</th><th class="awrbg" scope="col">Snap Time</th><th class="awrbg" scope="col">Sessions</th><th class="awrbg" scope="col">Cursors/Session</th></tr>
 <tr><td class="awrnc" scope="row">Begin Snap:</td><td align="right" class="awrnc">98810</td><td align="center" class="awrnc">29-Jun-15 08:00:02</td><td align="right" class="awrnc">700</td><td align="right" class="awrnc">    129.6</td></tr>
 <tr><td class="awrc" scope="row">End Snap:</td><td align="right" class="awrc">98864</td><td align="center" class="awrc">29-Jun-15 17:00:23</td><td align="right" class="awrc">703</td><td align="right" class="awrc">    129.1</td></tr>
 <tr><td class="awrnc" scope="row">Elapsed:</td><td class="awrnc"> </td><td align="center" class="awrnc">             540.35 (mins)</td><td class="awrnc"> </td><td class="awrnc"> </td></tr>
 <tr><td class="awrc" scope="row">DB Time:</td><td class="awrc"> </td><td align="center" class="awrc">           2,963.17 (mins)</td><td class="awrc"> </td><td class="awrc"> </td></tr>
 </tbody></table><table summary="This table displays snapshot information"></table><table summary="This table displays snapshot information"></table><body><html>"""

soup = bsoup(html)

list_of_tables = soup.find_all("table", {"summary":"This table displays snapshot information"}) # This will return 3 tables based on the above HTML.
target_table = list_of_tables[0] # Target the first one.

list_of_tds = target_table.find_all('td', class_='awrnc')
target_td = list_of_tds[2]
target_value = target_td.get_text()
print target_value
# 29-Jun-15 08:00:02

TL; DR:只需在列表中使用[0]即可。好像它是你找到的唯一一张桌子。之后,您可以再次在其中搜索,因为它变为有效的BeautifulSoup HTML字符串。