Removing new line character from Resultset (Python)
<p>I have ResultSet that contains information as below - </p>

<pre><code>[&lt;div id="Description"&gt;\n    This is the content example.\n\r\nThese characters I need to remove from complete string.\n\r\nI tried strip,lstrip,rstrip and replace.\n\r\nBut for these I found the Attributeerror: resultset object has no attribute 'strip'(lstrip/rstrip/replace).\n&lt;/div&gt;]
</code></pre>

<p>I retrieved it with:</p>

<pre><code>webPage=urllib2.urlopen(GivenUrl)
soup=BeautifulSoup(webPage,"html.parser")
soupResultSet=soup.findAll('div',id='Description') #This result set contains the above information.
</code></pre>

<p>I am using python 2.7.12.</p>

<p>The original HTML:</p>

<pre><code>&lt;div id="Description"&gt;
    This is the content example.
These characters I need to remove from complete string.
I tried strip,lstrip,rstrip and replace.
But for these I found the Attributeerror: resultset object has no attribute 'strip'(lstrip/rstrip/replace).
&lt;/div&gt;
</code></pre>

<p><code>ResultSet</code> is a simple <code>list</code> subclass. <code>str.strip()</code> does not exist on lists, nor on the <code>div</code> element.</p>

<p>Get the <em>text</em> from each element, you can use the <a href="https://www.crummy.com/software/BeautifulSoup/bs4/doc/#get-text" rel="nofollow"><code>Tag.get_text()</code> method</a>, which supports stripping directly:</p>

<pre><code>[tag.get_text(strip=True) for tag in soup.find_all('div', id='Description')]
</code></pre>

<p>Since you appear to be looking for a <code>&lt;div&gt;</code> with an <code>id</code> attribute, there should be only <em>one</em> such element. In that case, rather than use <code>soup.find_all()</code>, you should be using <code>soup.find()</code> and just get that one element rather than a list:</p>

<pre><code>soup.find('div', id='Description').get_text(strip=True)
</code></pre>

<p>This gives you <em>one</em> <code>str</code> object, with whitespace removed from the start and end. You can further process this if you need to remove all newlines from the middle of the string too.</p>

python beautifulsoup