Search code examples
pythonurllib

Having "urllib.error.URLError: <urlopen error unknown url type: https>" error when trying to automate script, but works fine running inside Spyder


So I have a script that reads tables off certain websites.

When I run this script inside Python IDE, it works perfectly. If I try to run it as a batch file independently, it doesn't work. It reflects the following error: urllib.error.URLError:

Any ideas as to what might cause it? My script are really short:

# -*- coding: utf-8 -*-
"""
Created on Sun Aug 18 20:24:04 2019

@author: blueb
"""
import requests
import numpy as np
import pandas as pd

dfs = pd.read_html("https://www.nea.gov.sg/dengue-zika/dengue/dengue-clusters")
count = 0
for df in dfs:
    count += 1
    print(df.head())

clusters_info = dfs[1]
clusters_info.to_csv('clusters.csv')


dls = "https://www.moh.gov.sg/docs/librariesprovider5/diseases-updates/weekly-infectious-bulletin_caseswk32y2019.xlsx?sfvrsn=cceaba67_0"
resp = requests.get(dls)

output = open('denguetrends.xlsx', 'wb')
output.write(resp.content)
output.close()

trends = pd.read_excel('denguetrends.xlsx')
trends.to_csv('denguetrends.csv')

Solution

  • Workaround use http if this page supports it. Real issue is the Python installation in the environment [broken SSL]:

    urllib HTTPS request: <urlopen error unknown url type: https>

    Did you build the Python environment where this is happening? or was it pre-packaged?.

    >>> import ssl
    ImportError: No module named ssl
    

    In any case, the Python interpreter that your Spyder installation is using, wasn't compiled with proper SSL support, or is not compatible with your system's SSL. Make sure you install SSL before installing Spyder. These are some resources that can help you: