如何管理html树?

时间:2016-11-18 08:52:21

标签: python beautifulsoup

<td style="width: 128px; border-top: none;">
<div class="pull-left" style="padding-right: 0.5em;">
<a href="/system/30001162/" rel="tooltip" title="V-3YG7">
<img class="eveimage img-rounded" src="https://     
 imageserver.eveonline.com/   Type/3802_64.png" width="64" height="64"   
 alt="V-3YG7" />
 </a>
 </div>
 </td>
 <td style="border-top: none;">
 <div itemscope="itemscope" class="pull-left">
 <table class="table table-condensed">
 <tbody>
 <tr>
 <th>System:</th>
 <td itemprop="systemname"><a href="/system/30001162/">V-3YG7</a></td>
 </tr><tr>
 <th>Region:</th>
 <td itemprop="region"><a href="/region/10000014/">Catch</a></td>
 </tr>
 </tbody>
 </table>
 </div>
 </td>
 <td itemprop="systemname"><a href="/system/30001162/">V-3YG7</a></td>
 <td itemprop="region"><a href="/region/10000014/">Catch</a></td>
 <td class="green" style="text-align: right" class="green-text">116,674</
 td>
 <td class="green hidden-xs" style="text-align: right">27</td>
 <td class="red" style="text-align: right">0</td>
 <td class="red hidden-xs" style="text-align: right">6,751</td>
 <td class="green hidden-xs" style="text-align: right">100.0</td>
 <td class="green" style="text-align: right">396,580</td>
 <td class="green hidden-xs" style="text-align: right">107</td>
 <td class="red" style="text-align: right">0</td>
 <td>
                  00:06
                            <small><a href="/kill/57241825/#comments" 
 data-disqus-identifier="57241825" rel="tooltip" title="Comments"></a></
 small>
 <br /><a href="/kill/57241825/">10.00k</a></td>
 <td class="icon hidden-xs" style="text-align: center; vertical-align: 
 middle;">
 <a href="/kill/57241825/" rel="tooltip" title="Detail for 57241825" 
 class="">
 <img src="https://imageserver.eveonline.com/Type/670_64.png" 
 height="40" width="40" class="eveimage img-rounded" alt="Capsule" />
 </a></td><td>
 <a href="/system/30001162/">V-3YG7</a> <span style="color: 
 #F30202">-0.1</span><br />
 <a href="/region/10000014/">Catch</a></td>
 <td class="hidden-xs" style="text-align: center; vertical-align: 
 middle; width: 64px;">
 <a href="/alliance/99005866/" rel="tooltip" title="Just let it happen">
 <img src="https://imageserver.eveonline.com/Alliance/99005866_64.png" 
 height="40" width="40" class="eveimage img-rounded" alt="Just let it 
 happen" /></a></td>
 <td class="victim" style="text-align: left; vertical-align: top;">
 <a href="/character/91628726/">stroon themighty</a> (Capsule)<br />
 <a href="/corporation/98423345/">Plaus Collective</a>
                  / <a href="/alliance/99005866/">Just let it happen</a>
 </td>
 <td class="hidden-xs" style="text-align: center; vertical-align: 
 middle; width: 64px;">
 <a href="/alliance/240835459/" rel="tooltip" title="The Volition Cult">
 <img src="https://imageserver.eveonline.com/Alliance/240835459_64.png" 
 height="40" width="40" class="eveimage img-rounded" alt="The Volition 
 Cult" /></a></td>

python代码:

for i in doc('td'):
print(i)

我用美丽的汤我得到了一些树,想要把它砍下来。 我试过了,但它没有用。

if i.div.a.img['class'] == 'vvvv':

我需要检查现有标签并将其删除或获取数据。它为什么不起作用?或者还有另一种方式吗?

我需要在课堂上获取标签文本&#39;受害者&#39;

2 个答案:

答案 0 :(得分:1)

请注意,每个TD上可能不存在所有节点,因此如果一个td内没有div,它将引发“AttributeError”,因为div为None,并且您尝试通过“i.div”访问其值。 a.img ......“等等。

另请注意,class属性可能包含多个类,因此您应该使用“in”而不是“==”。以下代码段应该可以正常工作。

beforeSelectRow

另请注意,img没有文本属性。

虽然,我宁愿使用“find_all”方法:

for i in doc('td'):
    if 'vvvv' in i.div.a.img['class'].split():
        print(i.div.a.img.text)

这将获得所有img与类'vvvv',请注意,如果它还有另一个类将无法正常工作。 https://www.crummy.com/software/BeautifulSoup/bs4/doc/#searching-by-css-class

快乐的编码。

答案 1 :(得分:0)

你需要找出你想要的东西:

  1. 如果要提取数据,则无需更改html文件
  2. 如果你想修改树,有很多方法可以做到这一点
  3. 您可以阅读BeautifulSoup了解更多信息。

相关问题