title: BeautifulSoup .children or .content without whitespace between tags
body: <p>I want all children of a tag without the whitespace between the tags. But BeautifulSoups <code>.contents</code> and <code>.children</code> also returns the whitespace between the tags.</p>
  
  <pre><code>from bs4 import BeautifulSoup
  html = """
  &lt;div id="list"&gt;
    &lt;span&gt;1&lt;/span&gt;
    &lt;a href="2.html"&gt;2&lt;/a&gt;
    &lt;a href="3.html"&gt;3&lt;/a&gt;
  &lt;/div&gt;
  """
  soup = BeautifulSoup(html, 'html.parser')
  print(soup.find(id='list').contents)
  </code></pre>
  
  <p>This prints:</p>
  
  <pre><code>['\n', &lt;span&gt;1&lt;/span&gt;, '\n', &lt;a href="2.html"&gt;2&lt;/a&gt;, '\n', &lt;a href="3.html"&gt;3&lt;/a&gt;, '\n']
  </code></pre>
  
  <p>Same with</p>
  
  <pre><code>print(list(soup.find(id='list').children))
  </code></pre>
  
  <p>What I want:</p>
  
  <pre><code>[&lt;span&gt;1&lt;/span&gt;, &lt;a href="2.html"&gt;2&lt;/a&gt;, &lt;a href="3.html"&gt;3&lt;/a&gt;]
  </code></pre>
  
  <p>Is there any way to tell BeautifulSoup to return only the tags and ignore the whitespace?</p>
  
  <p><a href="https://www.crummy.com/software/BeautifulSoup/bs4/doc/#contents-and-children" rel="nofollow noreferrer">The documentation</a> is not very helpful on this topic. The html in the example does not contain any whitespace between tags.</p>
  
  <p>Indeed stripping the html of all whitespace between tags solves my problem:</p>
  
  <pre><code>html = """&lt;div id="list"&gt;&lt;span&gt;1&lt;/span&gt;&lt;a href="2.html"&gt;2&lt;/a&gt;&lt;a href="3.html"&gt;3&lt;/a&gt;&lt;/div&gt;"""
  </code></pre>
  
  <p>Using this html I get the tags without whitespace between the tags because there is no whitespace between the tags. But I hoped to use BeautifoulSoup so I do not have to mess around in the html source code. I was hoping BeautifulSoup does that for me.</p>
  
  <p>Another workaround might be:</p>
  
  <pre><code>print(list(filter(lambda t: t != '\n', soup.find(id='list').contents)))
  </code></pre>
  
  <p>But that seems flaky. Is the whitespace guaranteed to be always exactly <code>'\n'</code>?</p>
  
  <hr>
  
  <p>A note to the duplicate marking brigade:</p>
  
  <p>There are many questions asking about BeautifulSoup and whitespace. Most are asking about getting rid of whitespace from the "rendered text".</p>
  
  <p>For example:</p>
  
  <p><a href="https://stackoverflow.com/questions/24558075/beautifulsoup-getting-rid-of-paragraph-whitespace-line-breaks">BeautifulSoup - getting rid of paragraph whitespace/line breaks</a></p>
  
  <p><a href="https://stackoverflow.com/questions/22890807/removing-new-line-n-from-the-output-of-python-beautifulsoup">Removing new line &#39;\n&#39; from the output of python BeautifulSoup</a></p>
  
  <p>Both questions want the text without whitespace. I want the tags without whitespace. The solutions there don't apply to my question.</p>
  
  <p>Another example:</p>
  
  <p><a href="https://stackoverflow.com/questions/38824121/regular-expression-for-class-with-whitespaces-using-beautifulsoup">Regular expression for class with whitespaces using Beautifulsoup</a></p>
  
  <p>This question is about whitespace in the class attribute.</p>