问题描述
代码:
import os
import tarfile
from urllib.request import urlretrieve
import pandas as pd
download_root = "https://raw.githubusercotent.com/ageron/handson-ml2/master/"
housing_path = os.path.join("datasets","housing")
housing_url = download_root + "datasets/housing/housing.tgz"
def fetch_housing_data(housing_url=housing_url,housing_path=housing_path):
os.makedirs(housing_path,exist_ok=True)
tgz_path = os.path.join(housing_path,"housing.tgz")
urlretrieve(housing_url,tgz_path)
housing_tgz = tarfile.open(tgz_path)
housing_tgz.extractall(path=housing_path)
housing_tgz.close()
def load_housing_data(housing_path=housing_path):
csv_path = os.path.join(housing_path,"housing.csv")
return pd.read_csv(csv_path)
fetch_housing_data()
housing = load_housing_data()
housing.head()
错误:urllib.error.URLError: <urlopen error [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time,or established connection failed because connected host has failed to respond>
我已经在VSCode和Jupyter笔记本中使用python文件尝试了此代码。我相信该错误是由于Windows上的代理/防火墙设置引起的。我尝试禁用Windows Defender防火墙,也启用和禁用了各种代理设置,但是没有运气。
大多数帖子对python2都有此错误-我还无法找到针对python3的修复程序。
解决方法
URL中看起来像是一个小的拼写错误,只是在“ githubusercontent”中缺少了一个“ n”
当前:
download_root = "https://raw.githubusercotent.com/ageron/handson-ml2/master/"
已修复:
download_root = "https://raw.githubusercontent.com/ageron/handson-ml2/master/"