fix bug where not checking for attrs != nil
This commit is contained in:
parent
605480dbfd
commit
f629ff784f
4
main.nim
4
main.nim
@ -10,7 +10,7 @@ var html: string
|
||||
#var node: XmlNode
|
||||
|
||||
echo "URL:"
|
||||
var url: string = readLine(stdin)
|
||||
var url: string = "https://www.comparis.ch/immobilien/marktplatz/lenzburg/mieten"#readLine(stdin)
|
||||
echo "given url is: ",url
|
||||
|
||||
try:
|
||||
@ -20,7 +20,7 @@ try:
|
||||
var htmlnode: XmlNode
|
||||
var entry : Entry
|
||||
|
||||
add(entry.desc,Descriptor(name : "", html_context_tag : "div",html_context_attrs : "class",html_context_key : "css-1d60yla ehesakb4", html_tag : "a",attrs : "href"))
|
||||
add(entry.desc,Descriptor(name : "test", html_context_tag : "div",html_context_attrs : "class",html_context_key : "css-1wvv8q", html_tag : "a",aattrs : "href"))
|
||||
#add(entry.desc,Descriptor(name:"key_and_string_search",contains_string: "a",html_context_tag : "div", html_tag : "a",attrs : "href"))
|
||||
|
||||
echo entry
|
||||
|
40
scrap.nim
40
scrap.nim
@ -11,7 +11,7 @@ type
|
||||
html_context_attrs* : string
|
||||
html_context_key* : string
|
||||
contains_string* : string
|
||||
attrs* : string
|
||||
aattrs* : string
|
||||
content* : seq[string]
|
||||
type
|
||||
Entry* = object
|
||||
@ -30,26 +30,34 @@ proc getEntryFromHtml*(entry :var Entry, node : XmlNode) : Entry =
|
||||
context = node.findAll(entry.desc[i].html_context_tag)
|
||||
else:
|
||||
tmpContext = node.findAll(entry.desc[i].html_context_tag)
|
||||
echo(tmpContext.len)
|
||||
#filter by attributes
|
||||
for i3,c in tmpContext:
|
||||
echo("c: ",c.attrs, "i3: ", i3, "i: ", i, "len tmpContext: ",tmpContext.len())
|
||||
if c.attrs.hasKey(entry.desc[i].html_context_attrs):
|
||||
echo("HAS-Context_KEY: ",c.attrs)
|
||||
if entry.desc[i].html_context_key != "":
|
||||
if c.attrs[entry.desc[i].html_context_attrs] == entry.desc[i].html_context_key:
|
||||
context.add(c)
|
||||
echo("key_MATCH")
|
||||
for c in tmpContext:
|
||||
echo c.len
|
||||
#echo("c: ",c.attrs, " i3: ", i3, " i: ", i, " len tmpContext: ", "entry: ",entry.desc[i])
|
||||
if entry.desc[i].html_context_attrs != "":
|
||||
if c.attrs != nil:
|
||||
if c.attrs.hasKey(entry.desc[i].html_context_attrs):
|
||||
echo("c.attrs[key]: ",c.attrs[entry.desc[i].html_context_attrs])
|
||||
if entry.desc[i].html_context_key != "":
|
||||
if entry.desc[i].html_context_key.contains(c.attrs[entry.desc[i].html_context_attrs]):
|
||||
context.add(c)
|
||||
echo("context_key_ MATCH")
|
||||
else:
|
||||
context.add(c)
|
||||
else:
|
||||
echo("c.attrs does not have the key")
|
||||
else:
|
||||
context.add(c)
|
||||
echo("no attributes on this tag")
|
||||
echo("out of for loop")
|
||||
#end filter context
|
||||
for a in context:
|
||||
let subContext = a.findAll(entry.desc[0].html_tag)
|
||||
let subContext = a.findAll(entry.desc[i].html_tag)
|
||||
for b in subContext:
|
||||
if entry.desc[i].attrs != "":
|
||||
if b.attrs.hasKey(entry.desc[i].attrs):
|
||||
echo("found key [",entry.desc[i].attrs,"] in : ",b)
|
||||
echo("value: ",b.attrs[entry.desc[i].attrs])
|
||||
if entry.desc[i].aattrs != "":
|
||||
if b.attrs.hasKey(entry.desc[i].aattrs):
|
||||
echo("found key [",entry.desc[i].aattrs,"] in : ",b)
|
||||
echo("value: ",b.attrs[entry.desc[i].aattrs])
|
||||
if entry.desc[i].contains_string != "":
|
||||
var content_txt :string = b.innerText()
|
||||
echo(" and string(innerText): ",content_txt)
|
||||
@ -57,7 +65,7 @@ proc getEntryFromHtml*(entry :var Entry, node : XmlNode) : Entry =
|
||||
echo("PATTERN MATCH")
|
||||
entry.desc[i].content.add(content_txt)
|
||||
else:
|
||||
entry.desc[i].content.add(b.attrs[entry.desc[i].attrs])
|
||||
entry.desc[i].content.add(b.attrs[entry.desc[i].aattrs])
|
||||
elif entry.desc[i].contains_string != "":
|
||||
var content_txt = b.innerText()
|
||||
echo(" string(innerText): ",content_txt)
|
||||
|
Loading…
Reference in New Issue
Block a user