使用 python 和 BeautifulSoup 从 html 中提取表格内容
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17196018/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Extracting table contents from html with python and BeautifulSoup
提问by Isaac
I want to extract certain information out of an html document. E.g. it contains a table (among other tables with other contents) like this:
我想从 html 文档中提取某些信息。例如,它包含一个表(在其他具有其他内容的表中),如下所示:
<table class="details">
<tr>
<th>Advisory:</th>
<td>RHBA-2013:0947-1</td>
</tr>
<tr>
<th>Type:</th>
<td>Bug Fix Advisory</td>
</tr>
<tr>
<th>Severity:</th>
<td>N/A</td>
</tr>
<tr>
<th>Issued on:</th>
<td>2013-06-13</td>
</tr>
<tr>
<th>Last updated on:</th>
<td>2013-06-13</td>
</tr>
<tr>
<th valign="top">Affected Products:</th>
<td><a href="#Red Hat Enterprise Linux ELS (v. 4)">Red Hat Enterprise Linux ELS (v. 4)</a></td>
</tr>
</table>
I want to extract Information like the date of "Issued on:". It looks like BeautifulSoup4 could do this easyly, but somehow I don't manage to get it right. My code so far:
我想提取诸如“发布日期:”之类的信息。看起来 BeautifulSoup4 可以很容易地做到这一点,但不知何故我无法做到这一点。到目前为止我的代码:
from bs4 import BeautifulSoup
soup=BeautifulSoup(unicodestring_containing_the_entire_htlm_doc)
table_tag=soup.table
if table_tag['class'] == ['details']:
print table_tag.tr.th.get_text() + " " + table_tag.tr.td.get_text()
a=table_tag.next_sibling
print unicode(a)
print table_tag.contents
This gets me the contents of the first table row, and also a listing of the contents. But the next sibling thing is not working right, I guess I am just using it wrong. Of course I could just parse the contents thingy, but it seems to me that beautiful soup was designed to prevent us from doing exactly this (if I start parsing myself, I might as well parse the whole doc ...). If someone could enlighten me on how to acomplish this, I would be gratefull. If there is a better way then BeautifulSoup, I would be interested to hear about it.
这让我得到第一个表格行的内容,以及内容列表。但是下一个兄弟的东西不能正常工作,我想我只是用错了。当然,我可以只解析内容,但在我看来,美丽的汤旨在阻止我们这样做(如果我开始解析自己,我不妨解析整个文档......)。如果有人能启发我如何实现这一点,我将不胜感激。如果有比 BeautifulSoup 更好的方法,我很想听听它。
采纳答案by falsetru
>>> from bs4 import BeautifulSoup
>>> soup = BeautifulSoup(unicodestring_containing_the_entire_htlm_doc)
>>> table = soup.find('table', {'class': 'details'})
>>> th = table.find('th', text='Issued on:')
>>> th
<th>Issued on:</th>
>>> td = th.findNext('td')
>>> td
<td>2013-06-13</td>
>>> td.text
u'2013-06-13'

