Can I combine two 'findAll' search blocks in beautifulsoup, into one?

beautifulsoup, python

Solution

You can pass functions to `.findall()` like this:

soup.findAll(lambda tag: tag.name in ['script', 'form'] or tag['id'] == "footer")

But you might be better off by first building a list of tags and then iterating over it:

tags = soup.findAll(['script', 'form'])
tags.extend(soup.findAll(id="footer"))

for tag in tags:
    tag.extract()

If you want to filter for several `id`s, you can use:

for tag in soup.findAll(lambda tag: tag.has_key('id') and
                                    tag['id'] in ['footer', 'content', 'links']):
    tag.extract()

A more specific approach would be to assign a lambda to the `id` parameter:

for tag in soup.findAll(id=lambda value: value in ['footer', 'content', 'links']):
    tag.extract()

Problem

Can I combine these two blocks into one: Edit: Any other method than combining loops like Yacoby did in the answer. ``` for tag in soup.findAll(['script', 'form']): tag.extract() for tag in soup.findAll(id="footer"): tag.extract() ``` Also can I multiple blocks into one: ``` for tag in soup.findAll(id="footer"): tag.extract() for tag in soup.findAll(id="content"): tag.extract() for tag in soup.findAll(id="links"): tag.extract() ``` or may be there is some lambda expression where I can check whether in array, or any other simpler method. Also how do I find tags with attribute class, as class is reserved keyword: EDIT: this part is solved by the soup.findAll(attrs={'class': 'noprint'}): ``` for tag in soup.findAll(class="noprint"): tag.extract() ```

Original source