通过AHK语言实现爬取i问财网站上的主动买入前100股票
i问财数据获取示例,抛砖引玉。
/*
功能: 1、爬取i问财主动买入比前100
2、
作者:sunwind1576157
日期:2021/11/7
*/
#SingleInstance,force
Gui, Add, Edit,R1 vMyEdit0 W480,主动买入比>主动卖出比从大到小排名前100
Gui, Add, Button, ys Default w80,确定
Gui, Add, Tab3,xs section , 主动买入比前100|同花顺人气100
Gui, Tab, 1
Gui, Add, Edit, R24 vMyEdit2 w550
Gui, Add, StatusBar,, 作者:sunwind1576157.
Gui, Show, x55 y66 w600 h440, 简单炒股 快乐生活
return
Button确定:
Gui,Submit,NoHide
主动买入比前100:=select_stock(MyEdit0)
GuiControl,, MyEdit2, %主动买入比前100%
return
select_stock(xg_tj="主动买入比>主动卖出比从大到小排名前100",hold_num:=200)
{
if (A_WDay=1)
dif:=-2
else if (A_WDay=7)
dif:=-1
else if (A_WDay=2)
dif:=-3
else
dif:=-1
endDate=%A_YYYY%%A_MM%%A_DD%
endDate += dif, days
StringLeft,endDate,endDate,8
SB_SetText("查询: " . xg_tj . " 中...")
urlxg :="http://backtest.10jqka.com.cn/tradebacktest/historypick?query=" . xg_tj . "&hold_num=" . hold_num . "&trade_date=" . endDate
headers:=""
xg:=URLDownloadToVar(urlxg,headers)
objxg:=JSON_ToObj(xg)
RowCount:=objxg.result.stocks.MaxIndex()
If (RowCount="")
RowCount:=0
SB_SetText("查询出: " . RowCount . " 个结果")
ret:=""
loop, %RowCount%
{
this_row:=A_index . "`t" . objxg.result.stocks[A_index].stock_code . "`t" . objxg.result.stocks[A_index].stock_name
ret.=this_row . "`n"
}
return ret
}
;======================辅助函数=================================
; Copyright 漏 2013 VxE. All rights reserved.
; Uses a two-pass iterative approach to deserialize a json string
json_toobj( str ) {
quot := """" ; firmcoded specifically for readability. Hardcode for (minor) performance gain
, ws := "`t`n`r " Chr(160) ; whitespace plus NBSP. This gets trimmed from the markup
, obj := {} ; dummy object
, objs := [] ; stack
, keys := [] ; stack
, isarrays := [] ; stack
, literals := [] ; queue
, y := nest := 0
; First pass swaps out literal strings so we can parse the markup easily
StringGetPos, z, str, %quot% ; initial seek
while !ErrorLevel
{
; Look for the non-literal quote that ends this string. Encode literal backslashes as '\u005C' because the
; '\u..' entities are decoded last and that prevents literal backslashes from borking normal characters
StringGetPos, x, str, %quot%,, % z + 1
while !ErrorLevel
{
StringMid, key, str, z + 2, x - z - 1
StringReplace, key, key, \\, \u005C, A
If SubStr( key, 0 ) != "\"
Break
StringGetPos, x, str, %quot%,, % x + 1
}
; StringReplace, str, str, %quot%%t%%quot%, %quot% ; this might corrupt the string
str := ( z ? SubStr( str, 1, z ) : "" ) quot SubStr( str, x + 2 ) ; this won't
; Decode entities
StringReplace, key, key, \%quot%, %quot%, A
StringReplace, key, key, \b, % Chr(08), A
StringReplace, key, key, \t, % A_Tab, A
StringReplace, key, key, \n, `n, A
StringReplace, key, key, \f, % Chr(12), A
StringReplace, key, key, \r, `r, A
StringReplace, key, key, \/, /, A
while y := InStr( key, "\u", 0, y + 1 )
if ( A_IsUnicode || Abs( "0x" SubStr( key, y + 2, 4 ) ) < 0x100 )
key := ( y = 1 ? "" : SubStr( key, 1, y - 1 ) ) Chr( "0x" SubStr( key, y + 2, 4 ) ) SubStr( key, y + 6 )
literals.insert(key)
StringGetPos, z, str, %quot%,, % z + 1 ; seek
}
; Second pass parses the markup and builds the object iteratively, swapping placeholders as they are encountered
key := isarray := 1
; The outer loop splits the blob into paths at markers where nest level decreases
Loop Parse, str, % "]}"
{
StringReplace, str, A_LoopField, [, [], A ; mark any array open-brackets
; This inner loop splits the path into segments at markers that signal nest level increases
Loop Parse, str, % "[{"
{
; The first segment might contain members that belong to the previous object
; Otherwise, push the previous object and key to their stacks and start a new object
if ( A_Index != 1 )
{
objs.insert( obj )
isarrays.insert( isarray )
keys.insert( key )
obj := {}
isarray := key := Asc( A_LoopField ) = 93
}
; arrrrays are made by pirates and they have index keys
if ( isarray )
{
Loop Parse, A_LoopField, `,, % ws "]"
if ( A_LoopField != "" )
obj[key++] := A_LoopField = quot ? literals.remove(1) : A_LoopField
}
; otherwise, parse the segment as key/value pairs
else
{
Loop Parse, A_LoopField, `,
Loop Parse, A_LoopField, :, % ws
if ( A_Index = 1 )
key := A_LoopField = quot ? literals.remove(1) : A_LoopField
else if ( A_Index = 2 && A_LoopField != "" )
obj[key] := A_LoopField = quot ? literals.remove(1) : A_LoopField
}
nest += A_Index > 1
} ; Loop Parse, str, % "[{"
If !--nest
Break
; Insert the newly closed object into the one on top of the stack, then pop the stack
pbj := obj
obj := objs.remove()
obj[key := keys.remove()] := pbj
If ( isarray := isarrays.remove() )
key++
} ; Loop Parse, str, % "]}"
Return obj
} ; json_toobj( str )
URLDownloadToVar(url, headers="", Encoding = "utf-8",Method="GET",postData=""){ ;网址,编码,请求方式,post数据
hObject:=ComObjCreate("WinHttp.WinHttpRequest.5.1")
try
{
hObject.SetTimeouts(30000,30000,1200000,1200000)
hObject.Open(Method,url,(Method="POST" ? True : False))
hObject.SetRequestHeader("Content-Type", "application/x-www-form-urlencoded")
if IsObject(headers)
{
for k,v in headers
hObject.SetRequestHeader(k, v)
}
hObject.Send(postData)
if (Method="POST")
hObject.WaitForResponse(-1)
}
catch e
return -1
if (Encoding && hObject.ResponseBody)
{
oADO := ComObjCreate("adodb.stream")
oADO.Type := 1
oADO.Mode := 3
oADO.Open()
oADO.Write(hObject.ResponseBody)
oADO.Position := 0
oADO.Type := 2
oADO.Charset := Encoding
return oADO.ReadText(), oADO.Close()
}
return hObject.ResponseText
}
;————————————————
;版权声明:本文为CSDN博主「liuyukuan」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
;原文链接:https://blog.csdn.net/liuyukuan/article/details/128598157
谢谢分享