Python的BeautifulSoup的findAll由"类"属性 [英] Python BeautifulSoup findAll by "class" attribute

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

问题描述

我要做到以下几点code,这是BS文件说,做,唯一的问题是,单词类不只是一个字。它可以在HTML里面可以找到,但它也导致此code抛出异常蟒蛇关键字。

那么,如何做到以下几点?

  soup.findAll(UL中,class =得分)


解决方案

您的问题似乎是,你期望 find_all 在汤中找到您的字符串精确匹配。 事实上


  

当您搜索匹配特定CSS类的标记,你
  对任何CSS类的匹配:


您可以正常搜索类标记作为@alKid说。您也可以搜索与类_ 关键字ARG。

  soup.find_all(UL,类_ =得分)

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.

So how do I do the following?

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

解决方案

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

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

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

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