Reassigning a return variable to a function in Python
Question: I am trying to figure out why I am getting a "SyntaxError: invalid syntax "
the variable title is highlighted red

    
    
    from urllib.request import urlopen
    from urllib.error import HTTPError
    from urllib.error import URLError
    from bs4 import BeautifulSoup
    def getTitle(url):
        try:
        html=urlopen(url)
        except HTTPError as e:
        return None
        try:
        bsObj=BeautifulSoup(html.read())
        title=bsObj.body.h1
        except AttributeError as e:
        return None
        return title
    title=getTitle("http://....html")
    


Answer: Python is very sensitive to whitespace, indentation, in order to keep your
code looking clean. From what I can see, you need to intent your try/except
statements:

    
    
    from urllib.request import urlopen
    from urllib.error import HTTPError
    from urllib.error import URLError
    from bs4 import BeautifulSoup
    def getTitle(url):
        try:
            html=urlopen(url)
        except HTTPError as e:
            return None
        try:
            bsObj=BeautifulSoup(html.read())
            title=bsObj.body.h1
        except AttributeError as e:
            return None
        return title
    title=getTitle("http://....html")
    

That should be it, do say if further errors occur.

