I want to change python's lambda formula back to the normal formula.

Asked 2 years ago, Updated 2 years ago, 75 views

There are some parts of the program on the site below where lambda is used, but I don't know what it or text refers to, although I think it means to substitute elements for comment objects.
Could you please let me know?

div=soup.find('div', class_='foo')
for element indiv (text=lambdate:isinstance(it, Comment)) :
    element.extract()

Code Source
https://itqna.net/questions/68805/remove-comment-tag-and-its-contents-beautifulsoup-4

python python3 beautifulsoup

2022-09-29 22:02

2 Answers

First, please look at the Python official documentation for the lambda formula.
6.14.Ramda(lambda)

The lambda formula (also known as lambda format) is used to create unknown functions. The expression lambda parameters:expression becomes a function object. This unknown object behaves like the function object defined below:

def<lambda>(parameters):
    return expression

See function definition for the syntax of the argument list.Note that functions created using lambda expressions cannot contain statements or annotations.

Here's an article with a little commentary:
How to use Python lambda (ramda expression, unknown function)

correspondence between def statements and lambda expressions
A correspondence between the function definition by the def statement and the unknown function in the corresponding lambda expression is as follows.For convenience, the name is assigned to the lambda expression (which substitutes the lambda expression for the variable), but is deprecated in Python's coding convention, PEP8, as described below.

def Name (argument, argument, ...):
    return expression

Name = lambda argument, argument, ...: expression

So it is the argument when viewed as a function.

For text, this description of the find_all() text argument will apply.

The text argument allows you to search for strings between tags.nameYou can use strings, regular expressions, lists, functions, and true values, such as arguments and keyword arguments.

The text argument is a search for a text string, but you can also combine a search for a tag. The Beautiful Soup finds all tags with the string specified by the text argument in .string.

In addition, this article will help you to return python's lambda formula to the normal formula, and to address the source code of the question.
Talks until you're upset about how to retrieve the contents of the comment tag in BeautifulSoup 4

The last source code I just applied is closed and parentheses are missing, so the program itself in the above article is an error, but if you apply it, the following parts of the question are:

for element indiv (text=lambdate:isinstance(it, Comment)):

This function definition and:

defis_comment(text):
    return isinstance (text, Comment)

This is the call:

for element indiv(text=is_comment):


2022-09-29 22:02

Click here for a Japanese translation of Beautiful Soup==>http://kondou.com/BS4/

The it in lambda might be meant to be aniterator/iterable? Only argument name.
As a function, we write tag here

defis_comment(tag):
  x = isinstance(tag, Comment)
  return x

all_comment=div.find_all(text=is_comment)
for elemin all_comment:
  em.extract()

The text argument says: "String, regular expression, list, function, True value is acceptable."
Specify the function in text=is_comment and search for the string between the tags.
In the function is_comment, return True if the element in question is a comment. Get a list of ==>comment elements?

(*Beautiful Soup 4.4.0 appears to be the string argument instead of the previous text argument.)

find_all is missing because shortcut


2022-09-29 22:02

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.