[英]Python Code to get data at set time intervals
我很好奇是否有人知道如何从http://www.fly.faa.gov/flyfaa/flyfaaindex.jsp?ARPT=SFO&p=0获得这样的结果: http://cdn.abclocal.go。 com / three / kgo / weather / links / SFO_DELAYS.txt我最初的效果很好,但实际延迟却没有,并给了我:
2016/7/11 23:35Z<b>30 minutes</b> in length and increasing. ,<b>General Arrival Delays: </b> Arrival traffic is experiencing airborne delays of 15 minutes or less. ,<strong>KJFK 112251Z 17009KT 10SM FEW055 SCT180 BKN260 23/16 A3008 RMK AO2 SLP186 T02280161</strong>
部分消息被截断。 另外,我很好奇是否有办法摆脱粗体和强者。
这是我的实际代码:#以下模块在线读取数据并将其导入到名为“ MetResearch”的电子表格中。numpy作为N导入lxml导入urllib2导入lxml.html导入html字符串导入时间
而True:
tree = html.fromstring(urllib2.urlopen('http://aviationweather.gov/adds/metars/index?station_ids=kjfk&std_trans=translated&chk_metars=on&hoursStr=most+recent+only&chk_tafs=on&submit=Submit').read())
td_text = tostring(tree.xpath('//strong')[1])
#print td_text[:]
tree1 = html.fromstring(urllib2.urlopen('http://www.fly.faa.gov/flyfaa/flyfaaindex.jsp?ARPT=JFK').read())
month= str(time.gmtime()[1])
day= str(time.gmtime()[2])
year= str(time.gmtime()[0])
hour= time.gmtime()[3]
minute= time.gmtime()[4]
td_text1 = tostring(tree1.xpath('//b')[-3])
td_text2 = tostring(tree1.xpath('//b')[-2])
f1=open('/home/jkalb/public_html/JFK.txt','a')
f1.write(month+"/"+day+"/"+year+" "+str(hour)+":"+str(minute)+"Z")
f1.write(td_text1+","+td_text2+","+td_text)
f1.close()
time.sleep(300)
关闭Canopy / VI窗口后,也无法使程序正常运行。
如果您想从faa.gov以html格式下载内容,则可以执行以下操作:
wget -O x.html http://www.fly.faa.gov/flyfaa/flyfaaindex.jsp?ARPT=SFO&p=0
为了使用您的python脚本处理x.html文件以获得一些SFO_DELAYS.txt输出文件。
对于已编辑的问题,现在的问题是如何每5分钟重复一次。 要么使用cron
,这是一个标准的unix(linux)实用程序,即可在Internet上获得大量信息,尤其是在unix教程中,例如here 。 例:
# Minute Hour Day of Month Month Day of Week Command
# (0-59) (0-23) (1-31) (1-12 or Jan-Dec) (0-6 or Sun-Sat)
0,5,10 * * * * /home/roland/wget.rc
其中,wget.rc是带有wget命令的小型Shell脚本。 (我不会尝试在cron选项卡中添加带有重定向等的复杂shell命令。)您可以将0,5,10扩展到...,50,55。
或使用无穷循环的脚本(例如在python或shell中),将其保存在类似get.rc的文件中:
#!/bin/sh
while true
do
wget ...
sleep 300
done
使文件可执行:
chmod +x get.rc
运行文件:
./get.rc
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.