# How to extract links from HTML

**URL:** <https://discourse.julialang.org/t/how-to-extract-links-from-html/91175>\
**Category:** General Usage\
**Created:** [December 3, 2022, 12:16pm UTC](https://discourse.julialang.org/t/how-to-extract-links-from-html/91175 "2022-12-03T12:16:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![edas](https://avatars.discourse-cdn.com/v4/letter/e/958977/32.png) [@edas](https://discourse.julialang.org/u/edas)\
**Post date:** [December 3, 2022, 12:16pm UTC](https://discourse.julialang.org/t/how-to-extract-links-from-html/91175/1 "2022-12-03T12:16:00Z")

</div>

I want to extract links form HTML

```julia
using Cascadia

using Gumbo

using HTTP,AbstractTrees

r = HTTP.get("https://example.com/")
h = parsehtml(String(r.body))
body = h.root[2]
link = eachmatch(Selector("a"), body)

```

Result:  
1-element Vector{HTMLNode}:  
HTMLElement{:a}:[  
More information…  
 ](https://www.iana.org/domains/example)  
Want to extract this line:  
[https://www.iana.org/domains/example](https://www.iana.org/domains/example)

Thanks,

---

<div class="post-metadata">

**Author:** ![cormullion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cormullion/32/49131_2.png) [@cormullion](https://discourse.julialang.org/u/cormullion)\
**Post date:** [December 3, 2022, 3:51pm UTC](https://discourse.julialang.org/t/how-to-extract-links-from-html/91175/2 "2022-12-03T15:51:38Z")

</div>

You can explore it at the REPL:

```julia
julia-1.8> link = eachmatch(Selector("a"), body)
1-element Vector{HTMLNode}:
 HTMLElement{:a}:<a href="https://www.iana.org/domains/example">
  More information...
</a>

julia-1.8> e1 = first(link)
HTMLElement{:a}:<a href="https://www.iana.org/domains/example">
  More information...
</a>

julia-1.8> e1. <TAB>
attributes children parent

julia-1.8> fieldnames(typeof(e1))
(:children, :parent, :attributes)

julia-1.8> e1.attributes
Dict{AbstractString, AbstractString} with 1 entry:
  "href" => "https://www.iana.org/domains/example"

julia-1.8> e1.attributes["href"]
"https://www.iana.org/domains/example"

julia-1.8> e1.parent
HTMLElement{:p}:<p>
  <a href="https://www.iana.org/domains/example">
    More information...
  </a>
</p>

julia-1.8> e1.attributes
Dict{AbstractString, AbstractString} with 1 entry:
  "href" => "https://www.iana.org/domains/example"

julia-1.8> e1.attributes["href"]
"https://www.iana.org/domains/example"

```

---

<div class="post-metadata">

**Author:** ![edas](https://avatars.discourse-cdn.com/v4/letter/e/958977/32.png) [@edas](https://discourse.julialang.org/u/edas)\
**Post date:** [December 4, 2022, 1:53pm UTC](https://discourse.julialang.org/t/how-to-extract-links-from-html/91175/3 "2022-12-04T13:53:46Z")

</div>

I expanded script according this code.  
It helped.  
Thanks 🙂
