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
scrapetest.py updated
  • Loading branch information
masamichiIto committed Aug 21, 2023
commit 79daf92fb161659ec9bfee5ca59f4f15db653435
17 changes: 14 additions & 3 deletions scrapetest.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
from urllib.request import urlopen
from urllib.error import HTTPError, URLError
from bs4 import BeautifulSoup

html = urlopen('http://pythonscraping.com/pages/page1.html')
try:
html = urlopen('http://pythonscraping.com/pages/page1.html')
except HTTPError as e:
print(e)
# return null, break, あるいは他の処理を実行
#print(html.read())
bs = BeautifulSoup(html.read(), 'html.parser')
print(bs.h1)
except URLError as e:
print("The server could not be found!")
else:
# プログラムは継続.
# ※例外の捕捉でreturnかbreakしたらelse文は実行されないため,いらない.
print("It worked!")
#bs = BeautifulSoup(html.read(), 'html.parser')
#print(bs.h1) # 上から最初のh1タグを取ってくる.複数h1タグがある場合は,最初のものしか取られないことに注意.