在 Python 中的 span 标签中查找多个属性 [英] Finding multiple attributes within the span tag in Python
本文介绍了在 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.
推荐答案
根据 docs,假设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 选择器,像这样:
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')
或者更复杂的东西,比如:
or something more sophisticated like:
items = [i for s in ['span.sp.starGryB', 'span.sp.starBig'] for i in soup.select(s)]
这篇关于在 Python 中的 span 标签中查找多个属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文