cezheng / Fuzi

A fast & lightweight XML & HTML parser in Swift with XPath & CSS support
MIT License
1.06k stars 151 forks source link

HTMLDocument is not ready immediately #111

Closed gazsiazasz closed 3 years ago

gazsiazasz commented 3 years ago

Description:

If I run the code below, sometimes it can not do the three xpath lookup, however the data can be dumped, and it contains a valid HTML.

Frequency: 3/10

Environment

How to reproduce:

    func parse(html: HTMLDocument) {
        let ads = html.xpath("//li[@data-uadid]")
        ads.forEach {
            if let data = try? Data(contentsOf: URL(string: $0.xpath(".//div[@class='uad-title']//a/@href").first!.stringValue)!) {
                let product = Product()
                let ad = try! HTMLDocument(data: data)
                product.product_id = ad.firstChild(xpath: "./@data-uadid")?.stringValue
                product.name = ad.firstChild(xpath: ".//div[@class='uad-title']//a/text()")?.stringValue
                product.url = ad.firstChild(xpath: ".//div[@class='uad-title']//a/@href")?.stringValue
                if let url = product.url {
                    print(url)
                } else {
                    print(product.product_id)
                    print($0.rawXML)
                }
            }
        }
    }
gazsiazasz commented 3 years ago

false alarm

mlch911 commented 9 months ago

how did you fix this? I'm encounter the same issue

mlch911 commented 9 months ago

In case anyone else gets the same error in the future, this error is because cNode memory may be freed. You must access the required properties when generating XMLElement.