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.

6
  • 1
    The error message doesn't correspond at all with the source code you posted. Line 12 is not the line containing the getTitle() call. Double check what code you're actually running? Commented Feb 22, 2017 at 18:12
  • That code doesn't produce a syntax error for me. Commented Feb 22, 2017 at 18:13
  • 1
    Are you running this from a command line interpreter? Commented Feb 22, 2017 at 18:18
  • 1
    try putting a blank line between line 11 & line 12 and run the script again Commented Feb 22, 2017 at 18:34
  • Tague Griffith, I might be. I'm going to link Python to Notepad++ and see if running it that way helps. Commented Feb 22, 2017 at 18:36

1 Answer 1

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?

Sign up to request clarification or add additional context in comments.

4 Comments

No, you don't have to do that. Where did you get this idea?
If you're using the command line interpreter, even though that is legal syntax, you will get a Syntax error.
This is correct. Try it. To end an indented block in the command line interpreter, Python needs a blank line.
Huh. I never noticed it before, because ipython doesn't have that restriction. Weird!!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.