Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion Please help to understand
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Иван Клешнин  
View profile  
 More options Aug 29 2012, 2:46 pm
From: Иван Клешнин <ivan.klesh...@gmail.com>
Date: Wed, 29 Aug 2012 11:46:59 -0700 (PDT)
Local: Wed, Aug 29 2012 2:46 pm
Subject: Please help to understand

I'm trying to remove all content-less tags from document. After node
removal it's parent should be reconsidered as content-less.

from bs4 import BeautifulSoup as bs

def normalize(soup):
    no_content_patt = re.compile(r'^[^\w]+$', re.U | re.S)

    while True:
        bad_blocks = soup(True, text=no_content_patt)
        if bad_blocks:
            for item in bad_blocks:
                item.decompose()
        else:
            break

# ... see below

soup = bs(s)
normalize(soup)
print soup.body.decode_contents()

What confuses me a lot, is a reaction to "\n" within tag's contens:

s = """
<p>Some content</p>       <-- keeps   (ok)
<p>...</p>                <-- removes (ok)
<p>!!<span>...</span></p> <-- removes (ok)
"""

...

s = """
<p>Some content</p>   <-- keeps (ok)
<p>...</p>            <-- removes (ok)
<p>!!<span>...</span> <-- keeps <p>!!\n</p> untouched (why?)
</p>
"""

So question is clear -- how can "\n" break my regex or soup behavior?


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.