我尝试使用以下脚本从FlightRadar24获取数据,基于this answer来处理Cookie。当我当前在浏览器中输入该URL时,我得到一个很好的长json或字典,包括lat / long / alt更新列表。但是,当我尝试下面的代码时,我收到下面列出的错误消息。
成功将json读入python需要做什么?
注意:该链接可能会在一两周内停止工作 - 他们不会永远提供数据。
import urllib2
import cookielib
jar = cookielib.FileCookieJar("cookies")
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(jar))
url = "http://lhr.data.fr24.com/_external/planedata_json.1.3.php?f=72c5ef5"
response = opener.open(url)
print response.headers
print "Got page"
print "Currently have %d cookies" % len(jar)
print jar
追踪(最近一次通话): 文件" [mypath] / test v00.py",第8行,in response = opener.open(链接) 文件" /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py" ;,第410行,打开 response = meth(req,response) 文件" /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py",第523行,http_response ' http',请求,响应,代码,消息,hdrs) File" /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py" ;,第448行,出错 return self._call_chain(* args) 文件" /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py",第382行,在_call_chain中 result = func(* args) 文件" /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py",第531行,http_error_default 提出HTTPError(req.get_full_url(),code,msg,hdrs,fp) HTTPError:HTTP错误403:禁止
答案 0 :(得分:2)
@AnandSKumar的第一个Answer是接受的答案,但是这里有一些有用的行,因为jsondata = response.read()
返回一个字符串。
注意:该链接可能会在一两周内停止工作 - 他们不会永远提供数据。
import urllib2
import json
import numpy as np
import matplotlib.pyplot as plt
# FROM this question: https://stackoverflow.com/a/32163003
# and THIS ANSWER: https://stackoverflow.com/a/32163003/3904031
# and a little from here: https://stackoverflow.com/a/6826511
url = "http://lhr.data.fr24.com/_external/planedata_json.1.3.php?f=72c5ef5"
req = urllib2.Request(url, headers={"Connection":"keep-alive", "User-Agent":"Mozilla/5.0"})
response = urllib2.urlopen(req)
the_dict = json.loads(response.read())
trail = the_dict['trail']
trailarray = np.array(trail)
s0, s1 = len(trailarray)/3, 3
lat, lon, alt = trailarray[:s0*s1].reshape(s0,s1).T
alt *= 10. # they drop the last zero
# plot raw data of the trail. Note there are gaps - no time information here
plt.figure()
plt.subplot(2,2,1)
plt.plot(lat)
plt.hold
plt.plot(lon)
plt.title('raw lat lon')
plt.subplot(2,2,3)
plt.plot(alt)
plt.title('raw alt')
plt.subplot(1,2,2)
plt.plot(lon, lat)
plt.title('raw lat vs lon')
plt.text(-40, 46, "this segment is")
plt.text(-40, 45.5, "transatlantic")
plt.text(-40, 45, "gap in data")
plt.savefig('raw lat lon alt')
plt.show()
将时间和日期信息转换为人形:
def humanize(seconds_since_epoch):
""" from https://stackoverflow.com/a/15953715/3904031 """
return datetime.datetime.fromtimestamp(seconds_since_epoch).strftime('%Y-%m-%d %H:%M:%S')
import datetime
humanize(the_dict['arrival'])
返回
'2015-08-20 17:43:50'