本文介绍了在Python的span标记中查找多个属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望从网站上抓取两个值.这些出现在以下标记中:

There are two values that i am looking to scrape from a website. These are present in the following tags:

<span class="sp starBig">4.1</span>
<span class="sp starGryB">2.9</span>

我需要值sp starBig,sp starGryB.

I need the values sp starBig, sp starGryB.

我正在使用的findAll表达式是-

The findAll expression that i am using is -

soup.findAll('span', {'class': ['sp starGryB', 'sp starBig']}):

代码被执行而没有任何错误,但没有显示结果.

The code gets executed without any errors yet no results get displayed.

推荐答案

按照文档,假设Beautiful Soup 4,使用像'sp starGryB'这样的字符串匹配多个CSS类是脆弱的,不应该这样做:

As per the docs, assuming Beautiful Soup 4, matching for multiple CSS classes with strings like 'sp starGryB' is brittle and should not be done:

soup.find_all('span', {'class': 'sp starGryB'})
# [<span class="sp starGryB">2.9</span>]
soup.find_all('span', {'class': 'starGryB sp'})
# []

应该像这样使用

CSS选择器 :

CSS selectors should be used instead, like so:

soup.select('span.sp.starGryB')
# [<span class="sp starGryB">2.9</span>]
soup.select('span.starGryB.sp')
# [<span class="sp starGryB">2.9</span>]

在您的情况下:

items = soup.select('span.sp.starGryB') + soup.select('span.sp.starBig')

或更复杂的东西:

items = [i for s in ['span.sp.starGryB', 'span.sp.starBig'] for i in soup.select(s)]

这篇关于在Python的span标记中查找多个属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 22:14