sdf
This commit is contained in:
parent
f849af778d
commit
cdf0a06595
39
main.nim
39
main.nim
@ -8,6 +8,7 @@ import std/json
|
|||||||
import std/strutils
|
import std/strutils
|
||||||
import std/tables
|
import std/tables
|
||||||
import std/macros
|
import std/macros
|
||||||
|
import std/enumerate
|
||||||
|
|
||||||
var client = newHttpClient()
|
var client = newHttpClient()
|
||||||
var html: string
|
var html: string
|
||||||
@ -36,42 +37,43 @@ proc dbg_al(level : int)=
|
|||||||
stdout.write level
|
stdout.write level
|
||||||
stdout.write("[array_loop]")
|
stdout.write("[array_loop]")
|
||||||
|
|
||||||
proc main_loop[T]( node : T , level : int, searchString : string = "",path :string = "H")
|
proc main_loop[T]( node : T , level : int, searchString : string = "",path :string = "")
|
||||||
|
|
||||||
proc do_loop[T](node: T,level : int, searchString : string = "", path: string = "", key : string) =
|
proc do_loop[T](node: T,level : int, searchString : string = "", path: string = "", key : string) =
|
||||||
if node.kind == JObject:
|
if node.kind == JObject:
|
||||||
for y in node.keys:
|
for y in node.keys:
|
||||||
if searchString == "" or path.contains(searchString):
|
if searchString == "" or path.contains(searchString) or y.contains(searchString):
|
||||||
dbg_dl(level)
|
dbg_dl(level)
|
||||||
stdout.write path & "-" & key & "-" & y," [", node[y].kind,"] {" ,key,"}","\n"
|
stdout.write path & "][" & key & "][" & y," [", node[y].kind,"] {" ,key,"}"
|
||||||
main_loop(node[y],level+1,searchString,path & "-" & key & "-" & y)
|
if node[y].kind == Jint:
|
||||||
|
stdout.write "(val: ", node[y],")\n"
|
||||||
|
else:
|
||||||
|
stdout.write "\n"
|
||||||
|
main_loop(node[y],level+1,searchString,path & "][" & key & "][" & y)
|
||||||
|
|
||||||
proc do_array_loop(node : JsonNode, level : int, searchString : string = "", path: string = "",key :string = "") =
|
proc do_array_loop(node : JsonNode, level : int, searchString : string = "", path: string = "",key :string = "") =
|
||||||
for b in node.getElems:
|
for i,b in enumerate(node.getElems):
|
||||||
if searchString == "" or path.contains(searchString) or key.contains(searchString):
|
if searchString == "" or path.contains(searchString) or b.getStr().contains(searchString):
|
||||||
dbg_al(level)
|
dbg_al(level)
|
||||||
echo "node : ", "array_node_len: ", b.len()," ,subkind: " ,b.kind, "mainkind: ", node.kind
|
echo "node : ", "array_node_len: ", b.len()," ,subkind: " ,b.kind, " mainkind: ", node.kind, "seq[",i,"]", " path:",path," key:",key
|
||||||
main_loop(b,level+1,searchString,path & key)
|
main_loop(b,level+1,searchString,path & "][" & key)
|
||||||
|
|
||||||
|
|
||||||
proc main_loop[T]( node : T , level : int, searchString : string = "",path :string = "H") =
|
proc main_loop[T]( node : T , level : int, searchString : string = "",path :string = "") =
|
||||||
if node.len() > 0:
|
if node.len() > 0:
|
||||||
if node.kind == JObject:
|
if node.kind == JObject:
|
||||||
var elem = node.getElems()
|
for i,b in enumerate(node.pairs):
|
||||||
for b in node.pairs:
|
|
||||||
if searchString == "" or path.contains(searchString) or b.key.contains(searchString):
|
if searchString == "" or path.contains(searchString) or b.key.contains(searchString):
|
||||||
dbg_ml(level)
|
dbg_ml(level)
|
||||||
echo "element: ",b, " path: ",path
|
echo "element:seq[",i,"]",b, " path: ",path
|
||||||
for a in node.keys:
|
for a in node.keys:
|
||||||
if node[a].kind == JObject:
|
if node[a].kind == JObject:
|
||||||
do_loop(node[a],level,searchString,path, a)
|
do_loop(node[a],level,searchString,path, a)
|
||||||
elif node[a].kind == JArray:
|
elif node[a].kind == JArray:
|
||||||
do_array_loop(node[a],level+1,searchString,path,a)
|
do_array_loop(node[a],level+1,searchString,path,a)
|
||||||
#elif node[a].kind == JString:
|
|
||||||
# dbg_ml(level)
|
|
||||||
# echo "String: ",path & "-" & $node[a]
|
|
||||||
elif node.kind == JArray:
|
elif node.kind == JArray:
|
||||||
do_array_loop(node,level,searchString,path)
|
do_array_loop(node,level+1,searchString,path)
|
||||||
|
|
||||||
|
|
||||||
echo "URL:"
|
echo "URL:"
|
||||||
var url: string = "https://www.comparis.ch/immobilien/marktplatz/lenzburg/mieten?page=2"#readLine(stdin)
|
var url: string = "https://www.comparis.ch/immobilien/marktplatz/lenzburg/mieten?page=2"#readLine(stdin)
|
||||||
@ -101,9 +103,10 @@ try:
|
|||||||
#echo "JSON Node:", jsonNode
|
#echo "JSON Node:", jsonNode
|
||||||
|
|
||||||
let field = jsonNode.getFields()
|
let field = jsonNode.getFields()
|
||||||
main_loop(jsonNode,0,searchString = "Title")
|
main_loop(jsonNode,0,searchString = "resultItems")
|
||||||
#-props-pageProps-initialResultData-resultItems
|
#-props-pageProps-initialResultData-resultItems
|
||||||
echo "node fields: ",jsonNode["props"]["pageProps"]["initialResultData"]["resultItems"][0]["Title"]
|
echo "node fields: ",jsonNode["props"]["pageProps"]["initialResultData"]["resultItems"][1]["Title"]
|
||||||
|
echo "node fields: ",jsonNode["props"]["pageProps"]["initialResultData"]["adIds"][0]
|
||||||
finally:
|
finally:
|
||||||
client.close()
|
client.close()
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user