Python BeautifulSoup 按“类"查找所有内容属性 [英] Python BeautifulSoup findAll by "class" attribute

查看:36
本文介绍了Python BeautifulSoup 按“类"查找所有内容属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做下面的代码,这是BS文档所说的,唯一的问题是类"这个词不仅仅是一个词.它可以在 HTML 中找到,但它也是一个导致此代码抛出错误的 python 关键字.

I want to do the following code, which is what BS documentation says to do, the only problem is that the word "class" isn't just a word. It can be found inside HTML, but it's also a python keyword which causes this code to throw an error.

那么我该怎么做呢?

soup.findAll('ul', class="score")

推荐答案

您的问题似乎是您希望 find_all 在汤中找到与您的字符串完全匹配的内容.事实上:

Your problem seems to be that you expect find_all in the soup to find an exact match for your string. In fact:

当您搜索与某个 CSS 类匹配的标签时,您会与其任何 CSS 类匹配:

When you search for a tag that matches a certain CSS class, you’re matching against any of its CSS classes:

您可以像@alKid 所说的那样正确搜索类标签.您还可以使用 class_ 关键字 arg 进行搜索.

You can properly search for a class tag as @alKid said. You can also search with the class_ keyword arg.

soup.find_all('ul', class_="score")

这篇关于Python BeautifulSoup 按“类"查找所有内容属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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