使用python和BeautifulSoup从html中提取表内容
我想从html文档中提取某些信息。它包含一个表 (在其他包含其他内容的表格中)如下所示:使用python和BeautifulSoup从html中提取表内容,python,beautifulsoup,screen-scraping,Python,Beautifulsoup,Screen Scraping,我想从html文档中提取某些信息。它包含一个表 (在其他包含其他内容的表格中)如下所示: <table class="details"> <tr> <th>Advisory:</th> <td>RHBA-2013:0947-1</td> </tr> <
<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>
这将获取第一个表行的内容,以及内容列表。
但下一个兄弟姐妹的事情是不工作的权利,我想我只是用错了。
当然,我可以把里面的东西解析出来,但在我看来,那汤很美
旨在阻止我们这样做(如果我开始自己解析,我可能会
好吧,解析整个文档。如果有人能告诉我如何完成这项工作,我
我会很感激的。如果有一个更好的方式,然后美化群山,我会感兴趣的
听一听。>>来自bs4 import BeautifulSoup
>>> 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'
>>>汤=美化组(包含整个文档的单销毁)
>>>table=soup.find('table',{'class':'details'})
>>>th=表。查找('th',text='发布日期:')
>>>th
发布日期:
>>>td=th.findNext('td')
>>>运输署
2013-06-13
>>>td.text
u'2013-06-13'
>>> 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'