gpt4 book ai didi

python - 使用 Python 抓取加拿大环境时出现 IOError

转载 作者:太空宇宙 更新时间:2023-11-03 16:33:34 24 4
gpt4 key购买 nike

为了工作,我试图从加拿大环境部网页获取批量数据,该网页实际上有自己的说明:ftp://ftp.tor.ec.gc.ca/Pub/Get_More_Data_Plus_de_donnees/Readme.txt当我运行代码时,我总是收到错误 10054;现有连接被远程主机强制关闭。作为一个相当新手的程序员,我想知道该网站是否不喜欢我的程序(我确实在省政府网站的早期阶段测试了该程序,它似乎检索了信息)或者我的代码中是否存在特定错误阻止我正确连接。欢迎提出如何继续的任何建议。谢谢

这是我的代码;最后一个 try/except block 是我在收到 IOError 消息后尝试重试连接:

import math
import datetime
import sys
import os
import urllib

# out_folder is relative to local directory
# station id is arbitrary; figure this out from the Web site
# by inspecting the URL of the stations Web page
[station, start_year, end_year, out_folder] = sys.argv[1:5]

print "retrieving data for station "+station+" for years "+start_year+" to "+end_year+" and saving in folder ./"+out_folder+"\n"

# generate filenames and download them
for year in range(int(start_year), int(end_year)+1):
for month in range(1, 2):

url = "http://climate.weather.gc.ca/climateData/bulkdata_e.html?format=csv&stationID="+str(station)+"&Year="+str(year)+"&Month="+str(month+1)+"&Day=1&timeframe=2&submit=Download+Data"
filename = 'stn_'+str(station)+'_'+str(year)+'.csv'
print 'stn_'+str(station)+'_'+str(year)+'.csv'
try:
print "Trying to retrieve data; please hold"
urllib.urlretrieve(url, out_folder+'\\'+filename)
except IOError:
os.mkdir(out_folder)
print "folder "+out_folder+" does not exist yet, creating it ...\n"
try:
print "Trying to retrieve data; please hold"
urllib.urlretrieve(url, out_folder+'\\'+filename)
except IOError:
print "Trying to retrieve data; please hold"
urllib.urlretrieve(url, out_folder+'\\'+filename)

exit()

此外,如果它有助于回溯的最后一行是:

File "C:\Python27\lib\socket.py", line 476 in readline

data = self._sock.recu(self._rbufsize)

IOError: [Errno socket error] [Errno 10054] An existing connection was forcibly...

最佳答案

抱歉造成不必要的询问;该问题与我自己的网络阻止连接有关,我切换到 Wifi 并且程序运行正常。

关于python - 使用 Python 抓取加拿大环境时出现 IOError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37418395/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com