nim-basic/scrap.nim

48 lines
1.3 KiB
Nim
Raw Normal View History

2024-06-27 11:55:39 +00:00
import std/htmlparser
import std/xmltree
import std/strtabs
2024-06-28 10:10:21 +00:00
type
2024-06-28 11:19:47 +00:00
Descriptor* = object
name* : string
html_tag* : string
html_context_tag* : string
contains_string* : string
attrs* : string
2024-06-27 10:02:25 +00:00
type
2024-06-28 11:19:47 +00:00
Entry* = object
2024-07-01 07:55:25 +00:00
desc* : seq[Descriptor]
2024-06-28 10:10:21 +00:00
2024-06-28 11:19:47 +00:00
proc getEntryFromHtml*(entry : Entry, node : XmlNode) =
2024-07-01 07:55:25 +00:00
for i,desc in entry.desc:
echo("descriptor ",i," content: ",entry.desc[i])
let context = node.findAll(entry.desc[i].html_context_tag)
for a in context:
let subContext = a.findAll(entry.desc[0].html_tag)
for b in subContext:
echo(b)
if entry.desc[i].attrs != "":
if b.attrs.hasKey(entry.desc[i].attrs):
echo("found key: ",entry.desc[i].attrs)
if entry.desc[i].contains_string != "":
echo(" and string: ",b)
#check if b contains contains_string
#return function
if entry.desc[i].contains_string != "":
echo("found string: ",b)
#check if b contains contains_string
#return function
2024-06-27 10:02:25 +00:00
2024-06-27 11:55:39 +00:00
2024-06-27 10:02:25 +00:00
proc test() =
2024-06-27 11:55:39 +00:00
var htmlnode : XmlNode
var str_html : string
2024-06-27 10:02:25 +00:00
var entry: Entry
2024-07-01 07:55:25 +00:00
entry.desc[0] = Descriptor(html_tag : "p", contains_string : "test_container")
2024-06-28 10:10:21 +00:00
echo entry
2024-06-27 10:02:25 +00:00
2024-06-27 11:55:39 +00:00
htmlnode = parseHtml(str_html)
2024-06-28 11:19:47 +00:00
entry.getEntryFromHtml(htmlnode)
2024-06-27 10:02:25 +00:00
2024-06-28 11:19:47 +00:00
#test()