Python BeautifulSoup does not print()
<p>Following beautifulsoup script shows no output. Did i miss anything?
It was intended to hit some of the prints.</p>

<pre><code>from urllib.request import urlopen
from urllib.error import HTTPError
from bs4 import BeautifulSoup
import sys

url1 = "https://www.youtube.com/watch?v=APmUWC8S1_M"

def getTitle(url):
    try:
        html = urlopen(url)
    except HTTPError as e:
        print(e)
        return None
    try:
        bsObj = BeautifulSoup(html.read())
    except AttributeError as e:
        return None
    return bsObj

    title = getTitle(url1)

    if title == None:
        print("None at URL: " + url1)
    else:
        print(title)
</code></pre>

<h3>EDIT:</h3>

<p>You problem is finally... identation.</p>



<pre><code>from urllib.request import urlopen
from urllib.error import HTTPError
from bs4 import BeautifulSoup
import sys

url1 = "https://www.youtube.com/watch?v=APmUWC8S1_M"

def getTitle(url):
    try:
        html = urlopen(url)
    except HTTPError as e:
        print(e)
        return None
    try:
        bsObj = BeautifulSoup(html.read())
    except AttributeError as e:
        return None
    return bsObj

title = getTitle(url1)

if title == None:
    print("None at URL: " + url1)
else:
    print(title)
</code></pre>

<h3>Old answer</h3>

<p>Your problem is that <code>return bsObj</code> prevent the function to execute the <code>print</code>s. The only thing your function can print is a <code>HTTPError</code> or an <code>ArgumentError</code>.</p>

<p>If you want to return <code>bsObj</code>, you need it to return it at the end of the function, because <code>return</code> exit the function.</p>

<p>Oh, and you inconditionally recurse the function, so anyway it will StackOverflow.</p>



<pre><code>from urllib.request import urlopen
from urllib.error import HTTPError
from bs4 import BeautifulSoup
import sys

url1 = "https://www.youtube.com/watch?v=APmUWC8S1_M"

def getTitle(url):
    try:
        html = urlopen(url)
    except HTTPError as e:
        print(e)
        return None
    try:
        bsObj = BeautifulSoup(html.read())
    except AttributeError as e:
        return None

    title = getTitle(url1) # Infinite recursion

    if title == None:
        print("None at URL: " + url1)
    else:
        print(title)
    return bsObj # Moved to the end
</code></pre>

python printing error-handling beautifulsoup