本文介绍了如何在BeautifulSoup中找到所有以字符串开头的类的div?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 BeautifulSoup 中,如果我想找到类为 span3 的所有 div,我会这样做:

result = soup.findAll("div",{"class":"span3"})

但是,在我的情况下,我想找到所有类以 span3 开头的 div,因此,BeautifulSoup 应该找到:

<div id="span3 span39">

等等……

我如何实现我想要的?我熟悉正则表达式;但是我不知道如何将它们实现到美丽的汤中,也没有通过浏览 BeautifulSoup 的文档找到任何帮助.

解决方案

好吧,这些是您正在显示的 id 属性:

<div id="span3 span39">

在这种情况下,您可以使用:

soup.find_all("div", id=lambda value: value 和 value.startswith("span3"))

或者:

soup.find_all("div", id=re.compile("^span3"))

如果这只是一个错字,并且您实际上有以 span3 开头的 class 属性,并且您确实需要检查以 span3,你可以使用starts-with"CSS选择器:

soup.select("div[class^=span3]")

这是因为您不能像检查 id 属性一样检查 class 属性,因为 class 是特殊的,它是一个 多值属性.

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"})

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">

And so on...

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.

解决方案

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"))

Or:

soup.find_all("div", id=re.compile("^span3"))


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]")

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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 05:53