如何在BeautifulSoup中查找所有班级以字符串开头的div? [英] How to find all divs who's class starts with a string in BeautifulSoup?
问题描述
在BeautifulSoup中,如果我想查找所有班级为span3的div,则只需执行以下操作:
In BeautifulSoup, if I want to find all div's where whose class is span3, I'd just do:
result = soup.findAll("div",{"class":"span3"})
但是,在我的情况下,我想查找所有以span3开头的div,因此BeautifulSoup应该找到:
However, in my case, I want to find all div's whose class starts with span3, therefore, BeautifulSoup should find:
<div id="span3 span49">
<div id="span3 span39">
依此类推...
我如何实现自己想要的?我熟悉正则表达式;但是我不知道如何将它们实现为漂亮的汤,也无法通过BeautifulSoup的文档找到任何帮助.
How do I achieve what I want? I am familiar with regular expressions; however I do not know how to implement them to beautiful soup nor did I find any help by going through BeautifulSoup's documentation.
推荐答案
好吧,这些是您正在显示的id
属性:
Well, these are id
attributes you are showing:
<div id="span3 span49">
<div id="span3 span39">
在这种情况下,您可以使用:
In this case, you can use:
soup.find_all("div", id=lambda value: value and value.startswith("span3"))
或者:
soup.find_all("div", id=re.compile("^span3"))
如果这只是一个错字,并且您实际上具有class
属性以span3
开头,并且您确实需要检查该类以span3
开头,则可以使用"starts-with" CSS选择器:
If this was just a typo, and you actually have class
attributes start with span3
, and your really need to check the class to start with span3
, you can use the "starts-with" CSS selector:
soup.select("div[class^=span3]")
这是因为您不能以与检查id
属性相同的方式检查class
属性,因为class
是特殊的,它是
This is because you cannot check the class
attribute the same way you checked the id
attribute because class
is special, it is a multi-valued attribute.
这篇关于如何在BeautifulSoup中查找所有班级以字符串开头的div?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!