Skip to content
This repository has been archived by the owner on Jan 13, 2024. It is now read-only.

Commit

Permalink
better error message
Browse files Browse the repository at this point in the history
  • Loading branch information
sdpython committed Dec 31, 2018
1 parent 3f93c72 commit 4f70389
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions src/pyquickhelper/loghelper/url_helper.py
Expand Up @@ -6,8 +6,18 @@

try:
import urllib.request as urllib_request
from urllib.error import HTTPError
except ImportError:
import urllib2 as urllib_request
from urllib2 import HTTPError


class CannotDownloadException(Exception):
"""
Raised by function @see fn get_url_content
if something cannot be downloaded.
"""
pass


def get_url_content(url, use_mozilla=False):
Expand All @@ -18,15 +28,23 @@ def get_url_content(url, use_mozilla=False):
@return page
"""
if use_mozilla:
req = urllib_request.Request(
url, headers={'User-agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64)' if sys.platform.startswith("win") else 'Mozilla/5.0'})
u = urllib_request.urlopen(req)
try:
req = urllib_request.Request(
url, headers={'User-agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64)' if sys.platform.startswith("win") else 'Mozilla/5.0'})
u = urllib_request.urlopen(req)
except HTTPError as e:
raise CannotDownloadException(
"Unable to download from url '{0}'".format(url)) from e
text = u.read()
u.close()
text = text.decode("utf8")
return text
else:
u = urllib_request.urlopen(url)
try:
u = urllib_request.urlopen(url)
except HTTPError as e:
raise CannotDownloadException(
"Unable to download from url '{0}'".format(url)) from e
text = u.read()
u.close()
text = text.decode("utf8")
Expand Down

0 comments on commit 4f70389

Please sign in to comment.