Skip to content

Presence of <script> in HTML Causing Parser to Miss Selection #67

Description

@defndaines

I have a script (shortened to minimum reproducible case below) where the selection for "table tr" fails to find the first <tr> tag. The example below only contains one <tr> tag, but I have other use cases where there are 9 tags, but only the last 8 are returned. But I have found that if I comment out the <script> from the HTML, then it does return the correct <tr> tag results.

(This is from a web scraper I've been playing with to pull information from goodreads.)

Test script:

localparser= {}
localhtmlparser=require("htmlparser")
functionparser.book_link(html, title, author)
localtree=htmlparser.parse(html)
localbooks=tree:select("table tr")
for_, bookinipairs(books) dolocalbook_title=book:select("a.bookTitle")
ifbook_title[1].nodes[1]:getcontent():match("^" ..title) thenlocalaut=book:select("a.authorName")
ifaut[1].nodes[1]:getcontent():match(author) thenreturn"https://www.goodreads.com" ..book_title[1].attributes["href"]:gsub("?.*", "")
endendendreturnnilendlocalfile=io.open("gr.html", "r")
localsearch_html=file:read("*a")
file:close()
localtitle="Waste Tide"localauthor="Chen Qiufan"localbook_link=parser.book_link(search_html, title, author)
assert("https://www.goodreads.com/book/show/39863294-waste-tide" ==book_link, "Incorrect book link: " ..book_link)

Test file:

<html><body><scripttype="text/javascript" charset="utf-8">functionrefreshGroupBox(group_id,book_id){newAjax.Updater('addGroupBooks'+book_id+'','/group/add_book_box',{asynchronous:true,evalScripts:true,onSuccess:function(request){refreshGroupBoxComplete(request,book_id);},parameters:'id='+group_id+'&book_id='+book_id+'&refresh=true'+'&authenticity_token='+encodeURIComponent('g0GG+Rcqg7zUv1eOBiN/m0Gxr1TlkcUeCyRfv9ZM7OGYokz03bxSNPCIOn1o7esOziTbneeb1ztimZdGK0srsg==')})}</script><table><tr><td><aclass="bookTitle" href="/book/show/39863294-waste-tide?from_search=true&amp;from_srp=true&amp;qid=WILgnaZ5jh&amp;rank=1"><spanitemprop='name'>Waste Tide</span></a><aclass="authorName"><spanitemprop="name">Chen Qiufan</span></a>,
</td></tr></table></body></html>

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions