Python 从类型';可导航字符串';和';标签';靓汤

Python 从类型';可导航字符串';和';标签';靓汤,python,beautifulsoup,Python,Beautifulsoup,我一直在分析烂西红柿网站的一部分,该网站将批评者的分数分别作为标签和“%”。我遵循了一些SO建议,比如使用find_all('span',text=“true”),但Python 3.5.1 shell返回了这个错误:AttributeError:'navigablesting'对象没有属性“find_all”我还尝试了查找Beautiful Soup对象的直接子对象critiscore,但收到了相同的错误。请告诉我哪里出错了。以下是我的python代码: def get_rating(addr

我一直在分析烂西红柿网站的一部分,该网站将批评者的分数分别作为标签和“%”。我遵循了一些SO建议,比如使用
find_all('span',text=“true”)
,但Python 3.5.1 shell返回了这个错误:
AttributeError:'navigablesting'对象没有属性“find_all”
我还尝试了查找Beautiful Soup对象的直接子对象
critiscore
,但收到了相同的错误。请告诉我哪里出错了。以下是我的python代码:

def get_rating(address):
    """pull ratings numbers from rotten tomatoes"""
    RTaddress = urllib.request.urlopen(address)
    tomatoe = BeautifulSoup(RTaddress, "lxml")
    for criticscore in tomatoe.find('span', class_=['meter-value superPageFontColor']):
        print(''.join(criticscore.find_all('span', recursive=False))) #print the Tomatometer
还有,这是我感兴趣的烂西红柿的代码:

<div class="critic-score meter">
                        <a href="#contentReviews" class="unstyled articleLink" id="tomato_meter_link">
                            <span class="meter-tomato icon big medium-xs certified_fresh pull-left"></span>
                            <span class="meter-value superPageFontColor"><span>96</span>%</span>
                        </a>
                    </div>

问题在于:

for criticscore in tomatoe.find('span', class_=['meter-value superPageFontColor']):
在这里,您通过
find()
定位单个元素,然后迭代它的子元素,这些子元素可以是文本节点,也可以是其他元素(当您迭代元素时,这就是
BeautifulSoup
中发生的情况)

相反,您可能打算使用
find_all()
而不是
find()

或者,您可以改为使用单个:


其中
表示直接的父子关系(这是您的
recursive=False
替换)。

起初我确实使用了find_all,但python用我不需要的同一类打印了另一个评级。有没有一种方法可以使用find_all()只打印第一个评级,或者使用find()打印评级和百分号?谢谢,我使用了css选择器并打印了列表中的第一个元素
for criticscore in tomatoe.find_all('span', class_=['meter-value superPageFontColor']):
for criticscore in tomatoe.select('span.meter-value > span'):
    print(criticscore.get_text())