看看这些标签和它们的属性:link
</tr> <tr data-has-trading-incentive="false" data-is-centralized="true">
</tr> <tr data-has-trading-incentive="false" data-is-centralized="true"> </tr> <tr data-is-centralized="true">
</tr> <tr data-has-trading-incentive="true">
</tr> <tr data-has-trading-incentive="false" data-is-centralized="false"> </tr> <tr data-has-trading-incentive="false">我想用python中的Beautifulsoup找到所有带有"tr“标签和属性的元素,就像下面的任何一个。
下面是我正在做的事情
soup.findAll("tr",attrs={re.compile("(data\-is\-centralized|data\-has\-trading-incentive)"):re.compile("(true|false)")})但这不会返回任何值。我做错了什么?有没有更好的方法来做这件事?
发布于 2021-04-08 18:53:23
您可以使用css OR语法对任一属性进行匹配
soup.select('[data-has-trading-incentive], [data-is-centralized]')发布于 2021-04-08 18:35:28
find_all也接受函数,其中函数默认将每个element作为其属性。
试试这个:
def findFilter(element):
if element.name == "tr":
if any(attr in list(element.attrs.values()) for attr in ['data-has-trading-incentive','data-is-centralized']):
return True
return False
filteredElements = soup.find_all(findFilter)https://stackoverflow.com/questions/67002025
复制相似问题