如何通过使用Beautifulsoup提取HTML表 [英] How to extract html table by using Beautifulsoup
本文介绍了如何通过使用Beautifulsoup提取HTML表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
考虑下面的HTML程式码片段为例:
>>>汤
<表>
< TR>< TD类=ABC>这是ABC< / TD>
< / TR>
< TR>< TD类=第一资讯> data1_xxx< / TD>
< / TR>
< /表><表>
&所述; TR>&下; TD类=EFG>这是EFG与所述; / TD>
< / TR>
< TR>< TD类=第一资讯> data1_xxx< / TD>
< / TR>
< /表>
如果我只能通过它的表数据类找到我的愿望表,
>>> soup.findAll(TD,{级:ABC})
[< TD类=ABC>这是ABC< / TD>]
我怎么能提取整个表如下?
<表>
< TR>< TD类=ABC>这是ABC< / TD>
< / TR>
< TR>< TD类=第一资讯> data1_xxx< / TD>
< / TR>
< /表>
解决方案
获得 D
标签的的 父
使用的 find_parent()
:
soup.find(TD,{级:ABC})。find_parent('表')
演示:
>>>从BS4进口BeautifulSoup
>>>数据=
...< DIV>
...<表>
...< TR>< TD类=ABC>这是ABC< / TD>
...< / TR>
...&所述; TR>&下; TD类=第一资讯> data1_xxx< / TD>
...< / TR>
...< /表>
...
...<表>
...&所述; TR>&下; TD类=EFG>这是EFG与所述; / TD>
...< / TR>
...&所述; TR>&下; TD类=第一资讯> data1_xxx< / TD>
...< / TR>
...< /表>
...< / DIV>
......
>>>汤= BeautifulSoup(数据)
>>>打印soup.find(TD,{级:ABC})。find_parent('表')
<表>
< TR>< TD类=ABC>这是ABC< / TD>
< / TR>
< TR>< TD类=第一资讯> data1_xxx< / TD>
< / TR>
< /表>
Taking the below html snippet as example:
>>>soup
<table>
<tr><td class="abc">This is ABC</td>
</tr>
<tr><td class="firstdata"> data1_xxx </td>
</tr>
</table>
<table>
<tr><td class="efg">This is EFG</td>
</tr>
<tr><td class="firstdata"> data1_xxx </td>
</tr>
</table>
If I can only find my desire table by its table data class,
>>>soup.findAll("td",{"class":"abc"})
[<td class="abc">This is ABC</td>]
how can I extract the whole table as below?
<table>
<tr><td class="abc">This is ABC</td>
</tr>
<tr><td class="firstdata"> data1_xxx </td>
</tr>
</table>
解决方案
Get the td
tag's parent
using find_parent()
:
soup.find("td", {"class":"abc"}).find_parent('table')
Demo:
>>> from bs4 import BeautifulSoup
>>> data = """
... <div>
... <table>
... <tr><td class="abc">This is ABC</td>
... </tr>
... <tr><td class="firstdata"> data1_xxx </td>
... </tr>
... </table>
...
... <table>
... <tr><td class="efg">This is EFG</td>
... </tr>
... <tr><td class="firstdata"> data1_xxx </td>
... </tr>
... </table>
... </div>
... """
>>> soup = BeautifulSoup(data)
>>> print soup.find("td", {"class":"abc"}).find_parent('table')
<table>
<tr><td class="abc">This is ABC</td>
</tr>
<tr><td class="firstdata"> data1_xxx </td>
</tr>
</table>
这篇关于如何通过使用Beautifulsoup提取HTML表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文