" 尝试自动化脚本时出错,但在 Spyder 中运行正常,python,urllib"/>

首页 > 解决方案 > 有“urllib.error.URLError:" 尝试自动化脚本时出错,但在 Spyder 中运行正常

问题描述

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

当我在 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')

标签: pythonurllib

解决方案


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

urllib HTTPS 请求:<urlopen 错误未知 url 类型:https>

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

>>> import ssl
ImportError: No module named ssl

Spyder在任何情况下,您的安装使用的 Python 解释器没有在适当的SSL支持下编译,或者与您系统的SSL. 确保在安装SSL之前安装Spyder. 以下是一些可以帮助您的资源:


推荐阅读