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

**URL:** <https://discourse.julialang.org/t/overcoming-boundserror-attempt-to-access-0-element-vector-htmlnode-at-index-1-during-a-webscrape/62102>\
**Category:** New to Julia\
**Created:** [May 30, 2021, 8:32pm UTC](https://discourse.julialang.org/t/overcoming-boundserror-attempt-to-access-0-element-vector-htmlnode-at-index-1-during-a-webscrape/62102 "2021-05-30T20:32:08Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nash](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nash/32/14482_2.png) [@Nash](https://discourse.julialang.org/u/Nash)\
**Post date:** [May 30, 2021, 8:32pm UTC](https://discourse.julialang.org/t/overcoming-boundserror-attempt-to-access-0-element-vector-htmlnode-at-index-1-during-a-webscrape/62102/1 "2021-05-30T20:32:08Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![contradict](https://avatars.discourse-cdn.com/v4/letter/c/ac91a4/32.png) [@contradict](https://discourse.julialang.org/u/contradict)\
**Post date:** [May 30, 2021, 9:07pm UTC](https://discourse.julialang.org/t/overcoming-boundserror-attempt-to-access-0-element-vector-htmlnode-at-index-1-during-a-webscrape/62102/2 "2021-05-30T21:07:32Z")

</div>

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

```julia
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

```

---

<div class="post-metadata">

**Author:** ![Skoffer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skoffer/32/378_2.png) [@Skoffer](https://discourse.julialang.org/u/Skoffer)\
**Post date:** [May 31, 2021, 3:10am UTC](https://discourse.julialang.org/t/overcoming-boundserror-attempt-to-access-0-element-vector-htmlnode-at-index-1-during-a-webscrape/62102/3 "2021-05-31T03:10:37Z")

</div>

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

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

# do stuff with a

```

---

<div class="post-metadata">

**Author:** ![Nash](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nash/32/14482_2.png) [@Nash](https://discourse.julialang.org/u/Nash)\
**Post date:** [May 31, 2021, 8:22am UTC](https://discourse.julialang.org/t/overcoming-boundserror-attempt-to-access-0-element-vector-htmlnode-at-index-1-during-a-webscrape/62102/4 "2021-05-31T08:22:27Z")

</div>

> [@contradict](#):
>
> ```julia
> if isempty(matches)
> continue # go back to the top of the enclosing loop
> else
> # do stuff with matches[1]
> end
> 
> ```

Unfortunately, I get:

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