tezro (tezro) wrote,
tezro
tezro

BeautifulSoup / How to remove or extract empty tags

The following code does not extract all the empty tags, though could be changed easily. All it does is removing empty or whitespaced P tags.


empty_tags = soup.findAll(lambda tag: tag.name == 'p' and not tag.contents and (tag.string is None or not tag.string.strip()))
[empty_tag.extract() for empty_tag in empty_tags]
Tags: code, python, source, исходник, код
Subscribe
  • Post a new comment

    Error

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

    When you submit the form an invisible reCAPTCHA check will be performed.
    You must follow the Privacy Policy and Google Terms of use.
  • 0 comments