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>

<p>For BeautifulSoup4, I would reccommend using the requests module (obtained via pip), for getting the website data.</p>

<p>To get the html of the desired site, use</p>

<pre><code>content = requests.get(url).content
</code></pre>

<p>That will save the entire html doc to the variable "content".</p>

<p>From that, you can get use the following script to print out any data you need.</p>

<p>Note: lxml (the html parser that is good for bs4) has problems when installing in python 3, so 2.7 is the best version for this.</p>

<pre><code>import requests
from bs4 import BeautifulSoup as bs

def getTitle(url):
    content = requests.get(url).content
    page = bs(content, "lxml")
    title = page.title.string
    return title

url1 = "https://www.youtube.com/watch?v=APmUWC8S1_M"
t = getTitle(url1)

if t == None:
    print "None at url " + url1
else:
    print t
</code></pre>

<p>I tested this on my local machine (Win 10, Python 2.7.12, requests, beautifulsoup4, and lxml installed via pip) and it worked perfectly.</p>

<p>If you want more information on requests, you can look <a href="http://docs.python-requests.org/en/master/" rel="nofollow">here</a>, and more info for BeautifulSoup can be found <a href="https://www.crummy.com/software/BeautifulSoup/bs4/doc/" rel="nofollow">here</a>.</p>

<p>Hope that this has helped you.</p>

python printing error-handling beautifulsoup