I am having a hard time understanding this code.
I would like to extract HTML comments using BeautifulSoup and Python3.
Given:
html = '''
<!-- Python is awesome -->
<!-- Lambda is confusing -->
<title>I don't grok it</title>
'''
soup = BeautifulSoup(html, 'html.parser')
I searched for solutions and most people said:
comments = soup.find_all(text= lambda text: isinstance(text, Comment))
Which in my case would result in:
[' Python is awesome ', ' Lambda is confusing ']
This is what I understand:
isinstanceasks iftextis an instance ofCommentand returns a boolean.- I sort of understand
lambda. Takestextas an argument and evaluates theisinstanceexpression. - You can pass a function to
find_all
This is what I do not understand:
- What is
textintext=? - What is
textinlambda text? - What argument from
htmlis passed intolambda text soup.textreturnsI don't grok it. Why islambda textpassing<!-- Python is awesome -->as an argument?