我正在尝试从国家气象局下载 GRIB 数据(二进制天气预报数据)。我已经编写了 Python 代码来格式化 HTTP 字符串以获取今天的数据,提前 12 小时。
Python 代码返回 HTTP 字符串,然后尝试使用 urllib.urlopen 下载数据。现在,如果我将 HTTP 字符串粘贴到 Firefox 中,则会下载 GRIB 文件。如果我尝试使用 urllib.urlopen,我会得到以下信息:
Traceback (most recent call last):
File "/home/dantayaga/bovine_aerospace/dev/grib_get.py", line 67, in <module>
webf=urllib.urlopen(griburl)
File "/usr/lib/python2.7/urllib.py", line 86, in urlopen
return opener.open(url)
File "/usr/lib/python2.7/urllib.py", line 207, in open
return getattr(self, name)(url)
File "/usr/lib/python2.7/urllib.py", line 344, in open_http
h.endheaders(data)
File "/usr/lib/python2.7/httplib.py", line 954, in endheaders
self._send_output(message_body)
File "/usr/lib/python2.7/httplib.py", line 814, in _send_output
self.send(msg)
File "/usr/lib/python2.7/httplib.py", line 776, in send
self.connect()
File "/usr/lib/python2.7/httplib.py", line 757, in connect
self.timeout, self.source_address)
File "/usr/lib/python2.7/socket.py", line 553, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
IOError: [Errno socket error] [Errno -2] Name or service not known
这是我正在使用的 HTTP 字符串:
如果您在 Firefox 中测试此字符串但它不起作用,请将“20120707”更改为今天的日期,将“06”更改为“00”,它应该可以工作。
我的问题很简单(我认为):为什么这适用于 Firefox 而不是 urllib?
这是我用来生成 http 字符串然后尝试下载结果的代码:
#Get GRIB files
import urllib
forecast_time='06' #What time the forecast is (00, 06, 12, 18)
forecast_hours='12' #How many hours ahead to forecast (2 or 3 digits)
forecast_date='20120707' #What date the forecast is for yyyymmdd
top_lat=90 #Top of bounding box (North)
bottom_lat=-90 #Bottom of bounding box (South)
left_lon=-90 #Left of bounding box (West)
right_lon=90 #Right of bounding box (East)
griburl='http://nomads.ncep.noaa.gov/cgi-bin/filter_gfs_hd.pl?'
griburl=griburl+'file=gfs.t'+str(forecast_time)+'z.mastergrb2f'
griburl=griburl+forecast_hours
#Select atmospheric levels
griburl=griburl+'&lev_1000_mb=on' #1000 mb level
griburl=griburl+'&lev_975_mb=on' #975 mb level
griburl=griburl+'&lev_950_mb=on' #950 mb level
griburl=griburl+'&lev_925_mb=on' #925 mb level
griburl=griburl+'&lev_900_mb=on' #900 mb level
griburl=griburl+'&lev_850_mb=on' #850 mb level
griburl=griburl+'&lev_800_mb=on' #800 mb level
griburl=griburl+'&lev_750_mb=on' #750 mb level
griburl=griburl+'&lev_700_mb=on' #700 mb level
griburl=griburl+'&lev_650_mb=on' #650 mb level
griburl=griburl+'&lev_600_mb=on' #600 mb level
griburl=griburl+'&lev_550_mb=on' #550 mb level
griburl=griburl+'&lev_500_mb=on' #500 mb level
griburl=griburl+'&lev_450_mb=on' #450 mb level
griburl=griburl+'&lev_400_mb=on' #400 mb level
griburl=griburl+'&lev_350_mb=on' #350 mb level
griburl=griburl+'&lev_300_mb=on' #300 mb level
griburl=griburl+'&lev_250_mb=on' #250 mb level
griburl=griburl+'&lev_200_mb=on' #200 mb level
griburl=griburl+'&lev_150_mb=on' #150 mb level
griburl=griburl+'&lev_100_mb=on' #100 mb level
griburl=griburl+'&lev_70_mb=on' #70 mb level
griburl=griburl+'&lev_30_mb=on' #30 mb level
griburl=griburl+'&lev_20_mb=on' #20 mb level
griburl=griburl+'&lev_10_mb=on' #10 mb level
#Select variables
griburl=griburl+'&var_HGT=on' #Height (geopotential m)
griburl=griburl+'&var_RH=on' #Relative humidity (%)
griburl=griburl+'&var_TMP=on' #Temperature (K)
griburl=griburl+'&var_UGRD=on' #East-West component of wind (m/s)
griburl=griburl+'&var_VGRD=on' #North-South component of wind (m/s)
griburl=griburl+'&var_VVEL=on' #Vertical Windspeed (Pa/s)
#Select bounding box
griburl=griburl+'leftlon='+str(left_lon)
griburl=griburl+'rightlon='+str(right_lon)
griburl=griburl+'toplat='+str(top_lat)
griburl=griburl+'bottomlat'+str(bottom_lat)
#Select date and time
griburl=griburl+'&dir=%2Fgfs.'+forecast_date+forecast_time+'%2Fmaster'
print(griburl)
print('Downloading GRIB file for date '+forecast_date+' time ' +forecast_time + ', forecasting '+forecast_hours+' hours ahead...')
webf=urllib.urlopen(griburl)
local_filename=forecast_date+'_'+forecast_time+'_'+forecast_hours+'.grib'
localf=open('//home//dantayaga//bovine_aerospace//grib//data//'+local_filename, 'wb')
localf.write(webf.read())
print('Requested grib data written to file '+local_filename)
非常感谢任何帮助。是否存在 Firefox 捕获的格式错误或其他问题?