Overcoming "BoundsError: attempt to access 0-element Vector{HTMLNode} at index [1]" during a webscrape

In the process of scraping data from a website, I use the following line of code:

a = eachmatch(Selector(".rank_view"), h.root)[1]

However, occasionally, the line throws the following error:

BoundsError: attempt to access 0-element Vector{HTMLNode} at index [1]

If I were to use an if-statement, how would I get my script to continue to the next iteration of my loop? I was hoping that I could use “if isempty(h.root)” but that does not work. How might I solve the stated problem?

I think you need to check if the match is empty, not the element:

matches = eachmatch(Selector(".rank_view"), h.root)
if isempty(matches)
    continue # go back to the top of the enclosing loop
else
    # do stuff with matches[1]
end

You can also use Cascadia.matchFirst (its not exported), it is either resulting Node or nothing.

a = Cascadia.matchFirst(sel".rank_view", h.root)
a === nothing && continue

# do stuff with a
1 Like

Unfortunately, I get:

MethodError: no method matching iterate(::HTMLElement{:p})