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.