[英]BeautifulSoup, difference between soup() and soup.findAll()?
I'm confused about the difference between soup('tag_name')
and soup.find_all('tag_name')
.我对
soup('tag_name')
soup.find_all('tag_name')
soup('tag_name')
和soup.find_all('tag_name')
soup('tag_name')
之间的区别感到困惑。 Here is an example with a short bit of html:这是一个带有一小段 html 的示例:
from bs4 import BeautifulSoup
string = """
<html><body><div class="MsoNormal">
<span style='font-family: "Times New Roman","serif"; font-size: 12.0pt;
line-height: 107%;'> Some text <o:p></o:p></span></div></body></html>
"""
soup = BeautifulSoup(string)
if soup('span') == soup.find_all('span'):
print('No difference')
This example is small, but I've tested much longer strings and found no difference between the two.这个例子很小,但我测试了更长的字符串,发现两者之间没有区别。 I thought it might be new as of
bs4
but all I could see in the documentation is that findAll
became find_all
.我认为从
bs4
它可能是新的,但我在文档中看到的只是findAll
变成了find_all
。 Are these two methods the same?这两种方法一样吗? Is the first one actually a method?
第一个实际上是一种方法吗? When will they give different results?
他们什么时候会给出不同的结果?
No, there is no difference between the two.不,两者之间没有区别。
From the documentation : " If you treat the BeautifulSoup object or a Tag object as though it were a function, then it's the same as calling find_all() on that object. "来自文档:“如果您将 BeautifulSoup 对象或 Tag 对象视为一个函数,那么它与在该对象上调用 find_all() 相同。 ”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.