定时获取上证指数成交量
本文展示AutoHotkey爬取网页的能力。
爬网页
WebRequest := ComObjCreate("WinHttp.WinHttpRequest.5.1")
WebRequest.Open("GET", "http://yunhq.sse.com.cn:32041/v1/sh1/list/self/000001?select=name%2Clast%2Camount")
WebRequest.Send()
result := WebRequest.ResponseText
结果形式
;{“date”:20171026,“time”:151500,“total”:1,“begin”:0,“end”:1,“list”:[[“上证指数”,3407.567,238596126821]]}
解析
o:=json2obj(result)
date:=o.date
time:=o.time
name:=o.list.0.0
last:=o.list.0.1
amount:=o.list.0.2
###格式化
last:=Round(last,2)
amount:= Round(amount/100000000,2)
time:=SubStr("0000000000" . time, -5)
FormatTime,time,%date%%time%,HH:mm
输出
ToolTip, 【%name%】`n当前价: %last%`n成交量: %amount%亿元`n更新时间: %time%
Sleep,3000
;~ 辅助函数
json2obj(s){
static parse:=comobjcreate("scriptcontrol")
parse.language:="jscript"
return parse.eval("(" s ")")
}
完整代码:
;~ 爬网页
WebRequest := ComObjCreate("WinHttp.WinHttpRequest.5.1")
WebRequest.Open("GET", "http://yunhq.sse.com.cn:32041/v1/sh1/list/self/000001?select=name%2Clast%2Camount")
WebRequest.Send()
result := WebRequest.ResponseText
;~结果形式
;~ {"date":20171026,"time":151500,"total":1,"begin":0,"end":1,"list":[["上证指数",3407.567,238596126821]]}
;~ 解析
o:=json2obj(result)
date:=o.date
time:=o.time
name:=o.list.0.0
last:=o.list.0.1
amount:=o.list.0.2
;~ 格式化
last:=Round(last,2)
amount:= Round(amount/100000000,2)
time:=SubStr("0000000000" . time, -5)
FormatTime,time,%date%%time%,HH:mm
;~ ; 输出
ToolTip, 【%name%】`n当前价: %last%`n成交量: %amount%亿元`n更新时间: %time%
Sleep,3000
;~ 辅助函数
json2obj(s){
static parse:=comobjcreate("scriptcontrol")
parse.language:="jscript"
return parse.eval("(" s ")")
}
热键请自行定义。可以定时获取,并输出到Excel中存储,以便用强大Excel工具来分析。
更新,增加定时获取能力,每半小时一次(参考cron语法):
这个表达式,表示在以下时间执行:
9:01
9:31
10:01
10:31
11:01
11:31
13:01
13:31
14:01
14:31
15:01
15:31