# How to read a table from url

**URL:** https://discourse.julialang.org/t/how-to-read-a-table-from-url/90985
**Category:** General Usage
**Tags:** dataframes, website, tables
**Created:** [November 29, 2022, 10:47am UTC](https://discourse.julialang.org/t/how-to-read-a-table-from-url/90985 "2022-11-29T10:47:56Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![empet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/empet/32/221303_2.png) [@empet](https://discourse.julialang.org/u/empet)
#### Post date: [November 29, 2022, 10:47am UTC](https://discourse.julialang.org/t/how-to-read-a-table-from-url/90985/1 "2022-11-29T10:47:56Z")

</div>

With pandas I can read a table into a dataframe, as follows:

```julia
import pandas as pd
dfs = pd.read_html('http://stats.ioinformatics.org/countries/')#returns a list of tables
df=dfs[0]
df.head()#inspect header

```

I searched for a similar method in DataFrames.jl (more precisely in the forthcomming book, Julia for Data Analysis, v\_10, which I have pre-ordered), as well in the Julia for Data Science, but there is no approach or example of such a task. Following a suggestion given as answer to the same question, posted here, two years ago: [[Any equivalent to Pandas read\_html() in DataFrames.jl?](https://discourse.julialang.org/t/any-equivalent-to-pandas-read-html-in-dataframes-jl/55674))([Any equivalent to Pandas read\_html() in DataFrames.jl?](https://discourse.julialang.org/t/any-equivalent-to-pandas-read-html-in-dataframes-jl/55674)), I tried something like this:

```julia
using DataFrames, CSV, HTTP
read_remote_csv(url) = DataFrame(CSV.File(HTTP.get(url).body))
df = read_remote_csv("http://stats.ioinformatics.org/countries/")

```

but it displays the html contents, not a dataframe having as columns the table columns.

---

<div class="post-metadata">

### Author: ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)
#### Post date: [November 29, 2022, 12:24pm UTC](https://discourse.julialang.org/t/how-to-read-a-table-from-url/90985/2 "2022-11-29T12:24:13Z")

</div>

My answer was for situations where the remote url returns a delimited file, which is what `CSV.File` parses. You are looking to extract a table from html, so need a library that parses html rather than delimited files, see:

> [@Scraping a html table from a website](https://discourse.julialang.org/t/scraping-a-html-table-from-a-website/47378):
>
> I am trying to obtain COVID-19 data from a website. The website has the data which I want but it is in a html table format I am looking for Julia tools to scrape the information from the html table. Something like this Is there an existing package in Julia for this? I am thinking about writing my own module to do this but I ask here first before writing my own module.

---

<div class="post-metadata">

### Author: ![empet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/empet/32/221303_2.png) [@empet](https://discourse.julialang.org/u/empet)
#### Post date: [November 29, 2022, 4:02pm UTC](https://discourse.julialang.org/t/how-to-read-a-table-from-url/90985/3 "2022-11-29T16:02:29Z")

</div>

Thank you for the link to Scraping a html table from a url. Unfortunately I have a low level of knowledge and skills in html/CSS. For the moment I will read the tables with `pd.read_html(url)`, save the corresponding dataframe as csv and re-read it in Julia.

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [November 29, 2022, 6:57pm UTC](https://discourse.julialang.org/t/how-to-read-a-table-from-url/90985/4 "2022-11-29T18:57:52Z")

</div>

Check also this post on [TableScraper.jl](https://discourse.julialang.org/t/ann-tablescraper-jl-an-easy-way-to-scrape-well-formed-tables-from-webpages/61636)

---

<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: [November 29, 2022, 7:11pm UTC](https://discourse.julialang.org/t/how-to-read-a-table-from-url/90985/5 "2022-11-29T19:11:08Z")

</div>

Beat me to it…

```julia
using TableScraper
using DataFrames

url = "https://stats.ioinformatics.org/countries/"

st = scrape_tables(url)

df = DataFrame(q=[], 
    Country=String[], 
    Host=String[],
    G=[],
    S=[],
    B=[],
    Total=[])

for row in first(st).rows
    push!(df, row)
end

df.G = parse.(Int, df.G)
df.S = parse.(Int, df.S)
df.B = parse.(Int, df.B)
df.Total = parse.(Int, df.Total)

```

```julia
109×7 DataFrame
 Row │ q Country Host G S B Total 
     │ Any String String Int64 Int64 Int64 Int64 
─────┼─────────────────────────────────────────────────────
   1 │ Albania 0 0 0 0
   2 │ ? Argentina 1993 3 9 23 35
  ⋮ │ ⋮ ⋮ ⋮ ⋮ ⋮ ⋮ ⋮
 108 │ ? Yugoslavia 1 3 1 5
 109 │ Zimbabwe 0 0 0 0
                                           105 rows omitted

```

---

<div class="post-metadata">

### Author: ![empet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/empet/32/221303_2.png) [@empet](https://discourse.julialang.org/u/empet)
#### Post date: [November 30, 2022, 11:03am UTC](https://discourse.julialang.org/t/how-to-read-a-table-from-url/90985/6 "2022-11-30T11:03:44Z")

</div>

@cormullion  
Thanks for your nice solution. I adopted it but replaced the last four lines of code by:

```julia
for name in names(df)[end-3:end]
    df[!, name]= parse.(Int, df[!, name])
end

```

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [November 30, 2022, 4:41pm UTC](https://discourse.julialang.org/t/how-to-read-a-table-from-url/90985/7 "2022-11-30T16:41:09Z")

</div>

Or without for loops:

```julia
using TableScraper, DataFrames
url = "https://stats.ioinformatics.org/countries/"
st = scrape_tables(url)
df = DataFrame(permutedims(reduce(hcat, first(st).rows)), [:q,:Country,:Host,:G,:S,:B,:Total])
df[!,[:G,:S,:B,:Total]] .= parse.(Int, df[!,[:G,:S,:B,:Total]])

```

---

<div class="post-metadata">

### Author: ![empet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/empet/32/221303_2.png) [@empet](https://discourse.julialang.org/u/empet)
#### Post date: [November 30, 2022, 5:31pm UTC](https://discourse.julialang.org/t/how-to-read-a-table-from-url/90985/8 "2022-11-30T17:31:00Z")

</div>

Yes, but the symbols :G, :S, :B, :Total, are repeated three times.

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [November 30, 2022, 5:44pm UTC](https://discourse.julialang.org/t/how-to-read-a-table-from-url/90985/9 "2022-11-30T17:44:08Z")

</div>

Could be replaced by `4:7`, or assigned to a single variable, for instance.
