2024-06-27 10:02:25 +00:00
|
|
|
import std/httpclient
|
|
|
|
import std/htmlparser
|
|
|
|
import std/xmltree
|
|
|
|
import std/strtabs
|
|
|
|
import std/os
|
2024-06-28 11:19:47 +00:00
|
|
|
import scrap
|
2024-07-03 12:03:25 +00:00
|
|
|
import std/json
|
|
|
|
import std/strutils
|
2024-06-27 10:02:25 +00:00
|
|
|
var client = newHttpClient()
|
|
|
|
var html: string
|
|
|
|
#var node: XmlNode
|
|
|
|
|
2024-07-04 12:02:46 +00:00
|
|
|
proc do_another_loop( node : JsonNode, level : int, searchString : string = "",path :string = "H") =
|
2024-07-03 12:03:25 +00:00
|
|
|
if node.len() > 0:
|
|
|
|
for a in node.keys:
|
|
|
|
if node.kind == JObject:
|
|
|
|
#for i in 0 .. level:
|
2024-07-04 12:02:46 +00:00
|
|
|
if searchString == "" or a.contains(searchString):
|
|
|
|
#for x in 0..level:
|
|
|
|
# stdout.write " "
|
|
|
|
stdout.write "|", path & "-" & a
|
2024-07-03 12:03:25 +00:00
|
|
|
if node[a].kind == JObject:
|
2024-07-04 12:02:46 +00:00
|
|
|
stdout.write("\n")
|
|
|
|
do_another_loop(node[a],level+1,searchString,path & "-" & a)
|
2024-07-03 12:03:25 +00:00
|
|
|
else:
|
2024-07-04 12:02:46 +00:00
|
|
|
if searchString == "" or a.contains(searchString):
|
|
|
|
stdout.write " [", node[a].kind, "]"
|
|
|
|
stdout.write("\n")
|
|
|
|
if node[a].kind == JArray:
|
|
|
|
if searchString == "" or a.contains(searchString):
|
|
|
|
echo "Array Elements: ", node[a].getElems()
|
|
|
|
echo "Array Length", node[a].len()
|
|
|
|
echo "WWWWWWWWWWWWWWW"
|
|
|
|
for b in node[a]:
|
|
|
|
echo b
|
|
|
|
do_another_loop(b,level,searchString,path & "-" & a)
|
|
|
|
|
|
|
|
#stdout.write "\n"
|
2024-07-03 12:03:25 +00:00
|
|
|
else:
|
|
|
|
echo "\n--------"
|
|
|
|
|
|
|
|
|
2024-06-27 10:02:25 +00:00
|
|
|
echo "URL:"
|
2024-07-02 12:00:19 +00:00
|
|
|
var url: string = "https://www.comparis.ch/immobilien/marktplatz/lenzburg/mieten?page=2"#readLine(stdin)
|
2024-06-27 10:02:25 +00:00
|
|
|
echo "given url is: ",url
|
|
|
|
|
|
|
|
try:
|
2024-06-28 11:19:47 +00:00
|
|
|
html = client.getContent(url)
|
|
|
|
let node = parseHtml(html)
|
|
|
|
|
|
|
|
var htmlnode: XmlNode
|
|
|
|
var entry : Entry
|
2024-06-28 11:58:36 +00:00
|
|
|
|
2024-07-02 12:00:19 +00:00
|
|
|
add(entry.desc,Descriptor(name : "test", html_context_tag : "div",html_context_attrs : "class",html_context_key : "css-19re50j", html_tag : "p",aattrs : "class",attrs_key : "css-svet6u"))
|
2024-07-03 12:03:25 +00:00
|
|
|
#comparis stores part of the data in a script tag as a json-string, so we get that part:
|
|
|
|
add(entry.desc,Descriptor(name : "script-json", html_context_tag : "script",html_context_attrs : "type",html_context_key : r"application/json", html_tag : "",aattrs : "",attrs_key : ""))
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-07-01 07:55:25 +00:00
|
|
|
|
2024-07-01 11:49:39 +00:00
|
|
|
echo("###########")
|
2024-07-03 12:03:25 +00:00
|
|
|
discard entry.getEntryFromHtml(node)
|
|
|
|
|
|
|
|
#echo entry.desc[1].content[0]
|
|
|
|
let jsonString = replaceWord(entry.desc[1].content[0],sub = r"\"",by = r""")
|
|
|
|
let jsonNode = parseJson(jsonString)
|
|
|
|
#echo "jsonString: ",jsonString
|
|
|
|
#echo "JSON Node:", jsonNode
|
|
|
|
#echo "node fields: ",jsonNode["props"]["pageProps"]["pageTitle"].getStr()
|
|
|
|
let field = jsonNode.getFields()
|
2024-07-04 12:02:46 +00:00
|
|
|
do_another_loop(jsonNode,0,searchString = "resultItems")
|
2024-07-03 12:03:25 +00:00
|
|
|
|
|
|
|
#echo "resultitem", jsonNode["resultItems"]
|
|
|
|
# for a in jsonNode.keys:
|
|
|
|
# echo a
|
|
|
|
# if jsonNode[a].len() > 0:
|
|
|
|
# for b in jsonNode[a].keys:
|
|
|
|
# echo ">>>"
|
|
|
|
# do_another_loop(jsonNode[a][b])
|
|
|
|
# else:
|
|
|
|
# echo "---------------"
|
2024-06-27 10:02:25 +00:00
|
|
|
finally:
|
|
|
|
client.close()
|
|
|
|
|
|
|
|
|
|
|
|
|