0

Day 1 Coding with Python. And with Soup. And in general. From the book: Web Scraping with Python: http://dl.finebook.ir/book/6f/13125.pdf

This is the code:

from urllib.request import urlopen
from urllib.error import HTTPError
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://www.pythonscraping.com/pages/page1.html")
if title == None:
    print("Title could not be found")
else:
    print(title)

This is the error:

File "<stdin>", line 12 title = getTitle("http://www.pythonscraping.com/pages/page1.html") Syntaxerror: invalid syntax

Little hat (^) under the 'e' in first title.

Using Python 3.4, Soup 4.

Thank you for your patience.

wim
  • 338,267
  • 99
  • 616
  • 750

1 Answers1

0

This code will generate an error if you try an execute it via the Python command line interpreter. Even though the code is syntactically correct by the Python language specification, the command line interpreter will flag that code with an error.

Adding whitespace between the end of your getTitle function definition and subsequent call on line 12 should solve your problem.

More information about function definition in Python can be found in this post: Python def function: How do you specify the end of the function?

Community
  • 1
  • 1
Tague Griffith
  • 3,963
  • 2
  • 20
  • 24