简体   繁体   中英

BeautifulSoup, difference between soup() and soup.findAll()?

I'm confused about the difference between soup('tag_name') and soup.find_all('tag_name') . Here is an example with a short bit of 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 . 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. "

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM