为什么Python BeautifulSoup对象是可调用的?定义在哪里?

问题描述 投票:0回答:1

bb和cc在下面的代码中相等,为什么会这样?汤是一个对象,为什么它可以在此处接受另一个参数“ a”?在此,汤('a')是函数调用还是其他类/对象初始化?如果是函数调用,则在类中没有找到call定义。我希望问题清楚。谢谢。

from bs4 import BeautifulSoup
soup = BeautifulSoup("<html><a href='bla'>sss</a><a>cc</a></html>", 'html.parser')
bb = soup('a')
cc = soup.find_all('a')
python beautifulsoup callable
1个回答
0
投票

嗯,实际上没有什么区别,将tag称为aliasfindAll

检查source code

def __call__(self, *args, **kwargs):
    """Calling a tag like a function is the same as calling its
    find_all() method. Eg. tag('a') returns a list of all the A tags
    found within this tag."""
    return self.find_all(*args, **kwargs)
© www.soinside.com 2019 - 2024. All rights reserved.