使用来自网站的rvest的表格提取与选择

时间:2017-04-26 06:08:09

标签: r checkbox rvest

我正在尝试从此页面获取表格:http://www.kase.kz/en/ticker/index 但是,在我走得更远之前,我将不得不选择一种乐器。我想选择以下两个:"哈萨克斯坦共和国国家银行"和#34;哈萨克斯坦共和国财政部"。

我尝试过以下代码:

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

我得到了:

list()

有什么建议吗?

更新

此代码似乎有效,但它以文本形式提供输出

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

2 个答案:

答案 0 :(得分:2)

如果您查看已删除HTML的DOM,那么表就在那里 - 当您在浏览器中查看页面时,它们就不在它们的位置。因此,通过一些调查,你可以找出一些选择器:

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    МЕОКАМ

...或者只是抓住所有表格,然后找出你需要的东西:

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

答案 1 :(得分:0)

到处玩,我遇到了XML包,它也完成了工作:

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