【问题标题】:Having "urllib.error.URLError: <urlopen error unknown url type: https>" error when trying to automate script, but works fine running inside Spyder尝试自动化脚本时出现“urllib.error.URLError: <urlopen error unknown url type: https>”错误,但在 Spyder 中运行良好
【发布时间】:2019-08-19 16:24:55
【问题描述】:

所以我有一个脚本可以从某些网站上读取表格。

当我在 Python IDE 中运行此脚本时,它运行良好。如果我尝试将它作为批处理文件独立运行,它就不起作用。它反映了以下错误: urllib.error.URLError:

关于什么可能导致它的任何想法?我的脚本很短:

# -*- 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')

【问题讨论】:

    标签: python urllib


    【解决方案1】:

    如果此页面支持,解决方法使用http。真正的问题是环境中的 Python 安装 [broken SSL]:

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

    您是否构建了发生这种情况的 Python 环境?还是预先包装好的?

    >>> import ssl
    ImportError: No module named ssl
    

    在任何情况下,您的Spyder 安装使用的 Python 解释器未在正确的SSL 支持下编译,或者与您系统的SSL 不兼容。确保在安装 Spyder 之前安装 SSL。这些资源可以帮助您:

    【讨论】:

    • 在 url 中使用“http”不会导致它。返回了相同的问题,因此看起来 url 正在自动重定向。你还有什么建议吗?我真的无法理解链接。
    • 我担心你会被重定向到https,因为大多数网站现在都这样做了。让我扩大一下答案。
    猜你喜欢
    • 2018-12-04
    • 1970-01-01
    • 2018-08-02
    • 2015-01-22
    • 2019-07-22
    • 2015-10-13
    • 1970-01-01
    • 2011-02-01
    • 2017-02-03
    相关资源
    最近更新 更多