python - 检查类是否等于两个字符串之一

标签 python python-2.7 beautifulsoup

是否有可能以某种方式修改它,以便它可以找到类 class1 *OR* 类 class2 的表?

Info = soup.find('table', {'class' :'class1'})

最佳答案

find_all(self, name=None, attrs={}, recursive=True, text=None, limit=None, **kwargs) 

    Extracts a list of Tag objects that match the given
    criteria.  You can specify the name of the Tag and any
    attributes you want the Tag to have.

    The value of a key-value pair in the 'attrs' map can be a
    string, a list of strings, a regular expression object, or a
    callable that takes a string and returns whether or not the
    string matches for some custom definition of 'matches'. The
    same is true of the tag name.

例如:

>>> from bs4 import BeautifulSoup
>>> text = ''.join('<table class="class{}"></table>'.format(i) for i in range(10))
>>> soup = BeautifulSoup(text)
>>> 
>>> soup.find_all("table", {"class": ["class1", "class7"]})
[<table class="class1"></table>, <table class="class7"></table>]
>>> import re
>>> soup.find_all("table", {"class": re.compile("class[17]")})
[<table class="class1"></table>, <table class="class7"></table>]
>>> 
>>> soup.find_all("table", {"class": lambda x: 3*int(x[-1])**2-24*int(x[-1])+17 == -4})
[<table class="class1"></table>, <table class="class7"></table>]

[好吧,最后一个匹配太多了,但你明白了:任何返回 bool 的匹配函数都可以。]

关于python - 检查类是否等于两个字符串之一,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13058889/

相关文章:

python - 将向量放入矩阵+变换中

python - 对 Python 列表中的每个元素执行字符串操作

python - 元组中的总和值(字典中的值)

python - 请求返回部分页面

python - 将 Python 与 mechanize 和 BeautifulSoup 捆绑在一起

javascript - 无法在 BeautifulSoup 中抓取一些细节

python - DJANGO:如何允许用户更改密码?

python - 如何使用python在Tensorboard上显示模型的权重和偏差

python-2.7 - plt.boxplot(data, vert = False) - 每个箱线图添加一个数据点 - python 2.7,Matplotlib 1.5.3

python - cx_Freeze 将 Python 和 Pygame 文档编译为 .exe 文件时出现奇怪的错误