AK88
AK88

Reputation: 3026

Table extraction using rvest from a website with selection

I am trying to get a table from this page here: http://www.kase.kz/en/ticker/index However, I will have to select a type of instruments before I go further. I would like to select the following two: "National Bank of the Republic of Kazakhstan" and "Ministry of Finance of the Republic of Kazakhstan".

I have tried the following code:

library(rvest)
p <- url %>%
read_html() %>%
html_nodes(xpath='//*[@id="pl1_11"]/table') %>%
html_table()

and I am getting:

list()

Any suggestions?

UPDATE

This code seems to work, but its giving the output in text form

url <- "http://www.kase.kz/en/ticker/index"
p <- url %>%
  read_html() %>%
  html_nodes(xpath='//td') %>%
  html_text()
p

Upvotes: 0

Views: 211

Answers (2)

AK88
AK88

Reputation: 3026

Playing around, I came across XML package, which also got the work done:

library(XML)
x = readHTMLTable('http://www.kase.kz/en/ticker/index')
Nat_Bank = x[[12]]
MF = x[[13]]

Upvotes: 0

alistaire
alistaire

Reputation: 43334

If you look at the DOM of the scraped HTML, the tables are there—just not where they are when you view the page in a browser. Thus, with a little investigation, you can figure out some selectors:

library(rvest)

p <- "http://www.kase.kz/en/ticker/index" %>% read_html()

nat_bank <- p %>% html_node('#pl1_10 + h2 + table') %>% html_table()

head(nat_bank)
#>          Code                                         Issuer  NIN or ISIN
#> 1 NTK007_1911 SI National Bank of the Republic of Kazakhstan KZW1KD079112
#> 2 NTK007_1914 SI National Bank of the Republic of Kazakhstan KZW1KD079146
#> 3 NTK007_1915 SI National Bank of the Republic of Kazakhstan KZW1KD079153
#> 4 NTK008_1913 SI National Bank of the Republic of Kazakhstan KZW1KD089137
#> 5 NTK028_1896 SI National Bank of the Republic of Kazakhstan KZW1KD288960
#> 6 NTK028_1903 SI National Bank of the Republic of Kazakhstan KZW1KD289034
#>             Type
#> 1 discount notes
#> 2 discount notes
#> 3 discount notes
#> 4 discount notes
#> 5 discount notes
#> 6 discount notes

min_of_fin <- p %>% html_node('#pl1_11 + h2 + table') %>% html_table()

head(min_of_fin)
#>          Code                                                Issuer
#> 1  KZ_05_2410 The Ministry of Finance of the Republic of Kazakhstan
#> 2  KZ_06_4410 The Ministry of Finance of the Republic of Kazakhstan
#> 3 MOM024_0085 The Ministry of Finance of the Republic of Kazakhstan
#> 4 MOM036_0087 The Ministry of Finance of the Republic of Kazakhstan
#> 5 MOM036_0088 The Ministry of Finance of the Republic of Kazakhstan
#> 6 MOM036_0089 The Ministry of Finance of the Republic of Kazakhstan
#>                NIN or ISIN      Type
#> 1 XS1120709669US486661AE13 eurobonds
#> 2 XS1120709826US486661AF87 eurobonds
#> 3             KZK2KY020859    МЕОКАМ
#> 4             KZK2KY030871    MEOKAM
#> 5             KZK2KY030882    MEOKAM
#> 6             KZK2KY030890    МЕОКАМ

...or just grab all the tables and figure out what you need later:

df_list <- p %>% html_nodes('table') %>% html_table(fill = TRUE)

df_list[[12]] %>% head()
#>          Code                                         Issuer  NIN or ISIN
#> 1 NTK007_1911 SI National Bank of the Republic of Kazakhstan KZW1KD079112
#> 2 NTK007_1914 SI National Bank of the Republic of Kazakhstan KZW1KD079146
#> 3 NTK007_1915 SI National Bank of the Republic of Kazakhstan KZW1KD079153
#> 4 NTK008_1913 SI National Bank of the Republic of Kazakhstan KZW1KD089137
#> 5 NTK028_1896 SI National Bank of the Republic of Kazakhstan KZW1KD288960
#> 6 NTK028_1903 SI National Bank of the Republic of Kazakhstan KZW1KD289034
#>             Type
#> 1 discount notes
#> 2 discount notes
#> 3 discount notes
#> 4 discount notes
#> 5 discount notes
#> 6 discount notes

Upvotes: 2

Related Questions