Read a title in HTML with Python script
<p>I have a small problem, I want to read a title in a HTML document, this is working so far, that I get the result of the string. Im using the libraray bs4 BeautifulSoup and urllib.request.</p>

<p><a href="http://i.stack.imgur.com/YuATM.png" rel="nofollow"><img src="http://i.stack.imgur.com/YuATM.png" alt="HTML Code"></a></p>

<p>You can see in the first image that the HTML code has a gap and this gap is even visible in the commandline, but I want only the title.
So how I can remove the HTML codes in the Output?</p>

<p><a href="http://i.stack.imgur.com/0W4cF.png" rel="nofollow"><img src="http://i.stack.imgur.com/0W4cF.png" alt="Command line Output"></a></p>

<p>Edit:
Here is the Python code you are looking for and which I have used</p>

<pre><code>import urllib.request
from bs4 import BeautifulSoup
import codecs

htmlfile = urllib.request.urlopen("https://www.packtpub.com/packt/offers/free-learning")

htmltext = htmlfile.read()

print(htmltext)


soup = BeautifulSoup(htmltext, 'html.parser')

print(soup)

f = codecs.open("freebook.html", "w", "utf-8")
f.write(soup.get())

f.close()
</code></pre>

<p>I hope this code help you</p>

<p>It is my understanding that you have the text content of an h2 tag in a variable, and you want to strip whitespace. So you can use <code>strip=true</code> in bs4 or <code>title = title.strip()</code>. </p>

python html beautifulsoup urllib bs4