python漂亮的汤获取HTML中所有类的名称 [英] python beautiful soup get name of all classes inside HTML

查看:113
本文介绍了python漂亮的汤获取HTML中所有类的名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用python和BS4获取标签内所有类的名称.

With python and BS4 I am trying to get the names of all the classes inside a tag.

到目前为止,我的代码是

So far my code is

    rows = table_body.find_all('tr')
        for row in rows:
            cols = row.find_all('td')
            for ele in cols:
                print ele.find_all('mark')

print result is [<mark class="footer"></mark>]

我怎样才能从美丽的汤中得类名呢? (我需要类的名称以供日后进行样式设置)

how can I get class name out of it with beautful soup. (I need the names of classes for later to do styling)

谢谢

推荐答案

如果您的问题中提到的那么简单,那就去做吧,

If it is as simple as mentioned in your question then just do,

print ele.find_all('mark')[0]['class']

但是,如果要过滤两个部分的元素-1.带类和2.不带类,则可以参考此

However, if you want to filter elements in two parts --- 1. with class and 2. without classes then you may refer to this link of bs4 documentation.

希望有帮助:-)

这篇关于python漂亮的汤获取HTML中所有类的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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