lundi 25 décembre 2017

urllib.error.URLError: HTTP Error 403: Forbidden from urllib.request.urlopen

I'm trying to get ulr status via urllib.request.urlopen and in some cases it return urllib.error.URLError: HTTP Error 403: Forbidden howewer I can open this url from browser successfully. Is it possible to overcome this problem with urllib or better to use some other lib?

def urllib_status(url):
    REQUEST_TIMEOUT = 10

    if 'http' not in url:
        url = 'http://' + url

    try:
        response = urllib.request.urlopen(url, timeout=REQUEST_TIMEOUT)
        return response.status
    except urllib.error.URLError as e:
        print('url:'+url)
        print('urllib.error.URLError:', e)
        return -1
    except ssl.SSLError as e:
        print('url:'+url)
        print('ssl.SSLError:', e)
        return -1
    except socket.error as e:
        print('url:'+url)
        print("socket.error: ", e)
        return -1




Aucun commentaire:

Enregistrer un commentaire