Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
scrapetest2.py, which includes try-excepetion procedure, was created
  • Loading branch information
masamichiIto committed Aug 21, 2023
commit b9ae315a2bebf4648ac3ae9271ea20bc6e5a7acd
21 changes: 21 additions & 0 deletions scrapetest2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from urllib.request import urlopen
from urllib.error import HTTPError, URLError
from bs4 import BeautifulSoup

def getTitle(url):
try:
html = urlopen(url)
except HTTPError as e:
return None
try:
bs = BeautifulSoup(html.read(), 'html.parser')
title = bs.body.h1
except AttributeError as e:
return None
return title

title = getTitle('http://pythonscraping.com/pages/page1.html')
if title == None:
print('Title could not be found')
else:
print(title)