728x90
from urllib.request import urlopen
from urllib.error import HTTPError
from bs4 import BeautifulSoup


def get_element(element, url):
try:
html = urlopen(url)
except HTTPError as e:
return None
try:
bs = BeautifulSoup(html.read(), 'html.parser')
result = getattr(bs, element).string
except AttributeError as e:
return None
return result


result = get_element('title', 'http://www.pythonscraping.com/pages/page1.html')
if result is None:
print('Title could not be found')
else:
print(result)

스크래핑이나 크롤링을 할 때 보면 여러가지 에러상황에 부딪히게 된다.

그 에러상황을 핸들링 하는 간단한 방법을 알아보자.


try:
html = urlopen(url)
except HTTPError as e:
return None

일단 html을 호출하는데 그 페이지가 현재 제대로됬는지 안됬는지 알아야한다.

위와 같이 try, except를 걸어준다.


try:
bs = BeautifulSoup(html.read(), 'html.parser')
result = getattr(bs, element).string
except AttributeError as e:
return None
return result

또한 bs의 에러캐치구문을 걸어준다.

bs에서 일어나는 에러는 여기서 잡아준다.


그 외에도 여러가지 에러 상황을 경험할 수 있으니 그 때마다 에러캐치를 해줘야 나중에 크롤링 봇을 만들 수 있다.

+ Recent posts