Вот кусок кода из книги:
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.div
except AttributeError as e:
return None
return title
title = getTitle("http://google.com")
if title == None:
print('Title could not be found')
else:
print(title)
Я так понял, если я передаю в urlopen("
google.com") оно проверяет если страница найдено то тогда все хорошо вернет html страницу.
А если страница не найдено возбуждается общий исключения HTTPError и я обработаю это исключения.
Иначе если сервер не найден я намеренно испорчу домен то тогда urlopen("
googlel.com") должен вернуть None.
Но умея так не работает а место него возвращает ошибку.
ссылка в книгу :
https://yadi.sk/i/YyfD45XS3KHiKA
28 страница в книге Райан Митчелл
Вот сам ошибка:
Traceback (most recent call last):
File "/usr/lib/python3.5/urllib/request.py", line 1254, in do_open
h.request(req.get_method(), req.selector, req.data, headers)
File "/usr/lib/python3.5/http/client.py", line 1106, in request
self._send_request(method, url, body, headers)
File "/usr/lib/python3.5/http/client.py", line 1151, in _send_request
self.endheaders(body)
File "/usr/lib/python3.5/http/client.py", line 1102, in endheaders
self._send_output(message_body)
File "/usr/lib/python3.5/http/client.py", line 934, in _send_output
self.send(msg)
File "/usr/lib/python3.5/http/client.py", line 877, in send
self.connect()
File "/usr/lib/python3.5/http/client.py", line 849, in connect
(self.host,self.port), self.timeout, self.source_address)
File "/usr/lib/python3.5/socket.py", line 693, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
File "/usr/lib/python3.5/socket.py", line 732, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno -5] No address associated with hostname
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/akmatali/project_akmatali/scraping/scraping.py", line 18, in
title = getTitle("
googlel.com")
File "/home/akmatali/project_akmatali/scraping/scraping.py", line 8, in getTitle
html = urlopen(url)
File "/usr/lib/python3.5/urllib/request.py", line 163, in urlopen
return opener.open(url, data, timeout)
File "/usr/lib/python3.5/urllib/request.py", line 466, in open
response = self._open(req, data)
File "/usr/lib/python3.5/urllib/request.py", line 484, in _open
'_open', req)
File "/usr/lib/python3.5/urllib/request.py", line 444, in _call_chain
result = func(*args)
File "/usr/lib/python3.5/urllib/request.py", line 1282, in http_open
return self.do_open(http.client.HTTPConnection, req)
File "/usr/lib/python3.5/urllib/request.py", line 1256, in do_open
raise URLError(err)
urllib.error.URLError: