替换类名BeautifulSoup [英] Replacing class name BeautifulSoup

查看:98
本文介绍了替换类名BeautifulSoup的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试解析HTML文档,并且想知道你们是否可以帮助我.

I'm trying to parse an HTML document, and was wondering if you guys can help me out.

<tr height="21" style="height:15.75pt">
       <td class="style14" height="21" style="height: 15.75pt">
        71
       </td>
       <td class="style14">
        Breakeven
       </td>
       <td class="style10">
        The Script
        <span style="mso-spacerun:yes">
        </span>
       </td>
      </tr>

我想将td class ='style10'更改为class ='style14'.但是,当我将其更改为style14时,它不会拾取它.因此,脚本"不会被打印出来.

I would like to change td class= 'style10' to class='style14'. However, when I change it to style14, it does not pick it up. So, "The Script" doesn't get printed.

这是我的代码:

search =soup.find('td', class_='style10')
search['class'] = 'style14'

for each in search: 
    print each.text

有没有办法做到这一点?

Is there a way to do this?

推荐答案

您正在遍历一个元素,并且仅列出了子元素.因为您选择的标签没有带有其他文本的子元素(<span style="mso-spacerun:yes">元素为空),所以您什么也看不到.

You are looping over one element, and that only lists child elements. Because your selected tag has no child elements with further text (the <span style="mso-spacerun:yes"> element is empty), you don't see anything.

请不要循环播放,直接进入文本:

Just don't loop, get to the text directly:

print search.text

您的班级变更在这里没有任何改变.

Your class change didn't break anything here.

演示:

>>> from bs4 import BeautifulSoup
>>> soup = BeautifulSoup('''\
... <tr height="21" style="height:15.75pt">
...        <td class="style14" height="21" style="height: 15.75pt">
...         71
...        </td>
...        <td class="style14">
...         Breakeven
...        </td>
...        <td class="style10">
...         The Script
...         <span style="mso-spacerun:yes">
...         </span>
...        </td>
...       </tr>
... ''')
>>> search =soup.find('td', class_='style10')
>>> search['class']
['style10']
>>> search['class'] = 'style14'
>>> search['class']
'style14'
>>> list(search)
[u'\n        The Script\n        ', <span style="mso-spacerun:yes">
</span>, u'\n']
>>> search.text
u'\n        The Script\n        \n\n'

这篇关于替换类名BeautifulSoup的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆