2

The tables I would like to scrape have url's in them. If I run the code, I get only the column with description of url. How to get the table which actually has a column (in mycase the second column) with URLs instead of their descriptions), or having a full html code of an anchor?. I need it to extract two index codes from the URL's in the second column of table. The links that I would like to scrape look like: https://aplikacje.nfz.gov.pl/umowy/Agreements/GetAgreements?ROK=2017&ServiceType=03&ProviderId=20795&OW=15&OrthopedicSupply=False&Code=150000001 and I need ProviderId and Code numbers but fist I need the links in the table scraped by the code below.

table<-0
library(rvest)
for (i in 1:10){
  url<-paste0("https://aplikacje.nfz.gov.pl/umowy/Provider/Index?ROK=2017&OW=15&ServiceType=03&OrthopedicSupply=False&page=",i)
  page<-html_session(url)
  table[i]<-html_table(page)
}

Thanks for all the comments and help.

Jacek Kotowski
  • 620
  • 16
  • 49

2 Answers2

3

Here's how to get them from one page. Note that I use the %>% operator, which makes for easier reading, but you could just nest the calls if you prefer

library(rvest)
url<-paste0("https://aplikacje.nfz.gov.pl/umowy/Provider/Index?ROK=2017&OW=15&ServiceType=03&OrthopedicSupply=False&page=1")
page<-html_session(url)
links <- html_nodes(page, 'table') %>% html_nodes("a") %>% html_attr("href")
Andrew Lavers
  • 4,328
  • 1
  • 12
  • 19
3

This shld help get a nice, clean, complete table with the hrefs you want:

library(rvest)
library(tidyverse)

# Helpers
rm_extra <- function(x) { gsub("\r.*$", "", x) }

mk_gd_col_names <- function(x) {
  tolower(x) %>%
    gsub("\ +", "_", .)
}

URL <- "https://aplikacje.nfz.gov.pl/umowy/Provider/Index?ROK=2017&OW=15&ServiceType=03&OrthopedicSupply=False&page=%d"

get_table <- function(page_num = 1) {

  pg <- read_html(sprintf(URL, page_num))

  tab <- html_nodes(pg, "table")

  html_table(tab)[[1]][,-c(1,11)] %>%
    set_names(rm_extra(colnames(.) %>% mk_gd_col_names)) %>%
    mutate_all(funs(rm_extra)) %>%
    mutate(link = html_nodes(tab, xpath=".//td[2]/a") %>% html_attr("href")) %>%
    as_tibble()

}

pb <- progress_estimated(10)
map_df(1:10, function(i) {
  pb$tick()$print()
  get_table(page_num = i)
}) -> full_df

glimpse(full_df)
## Observations: 93
## Variables: 10
## $ kod                         <chr> "150000016", "150005039", "1500046...
## $ nazwa_świadczeniodawcy      <chr> "SAMODZIELNY PUBLICZNY ZAKŁAD OPIE...
## $ miasto                      <chr> "GRODZISK WIELKOPOLSKI", "KALISZ",...
## $ ulica                       <chr> "MOSSEGO 17", "POZNAŃSKA 23", "OS....
## $ kod_pocztowy                <chr> "62065", "62800", "60688", "62510"...
## $ nip                         <chr> "9950036856", "6181976770", "97201...
## $ regon                       <chr> "317760", "251525840", "630804009"...
## $ sumaryczna_kwota_zobowiązań <chr> "8 432 922,00", "332 078,25", "416...
## $ szczegóły                   <chr> "Umowy", "Umowy", "Umowy", "Umowy"...
## $ link                        <chr> "/umowy/Agreements/GetAgreements?R...

full_df
## # A tibble: 93 × 10
##          kod
##        <chr>
## 1  150000016
## 2  150005039
## 3  150004658
## 4  150009135
## 5  150003546
## 6  150000066
## 7  150003556
## 8  150000073
## 9  150003539
## 10 150008909
## # ... with 83 more rows, and 9 more variables:
## #   nazwa_świadczeniodawcy <chr>, miasto <chr>, ulica <chr>,
## #   kod_pocztowy <chr>, nip <chr>, regon <chr>,
## #   sumaryczna_kwota_zobowiązań <chr>, szczegóły <chr>, link <chr>
hrbrmstr
  • 77,368
  • 11
  • 139
  • 205
  • Looks great but it did not work well on my machine. It throws at me: `Error in open.connection(x, "rb") : Peer certificate cannot be authenticated with given CA certificates` – Jacek Kotowski Apr 11 '17 at 18:04
  • Now it works after I used httr::GET trick from your comment! http://stackoverflow.com/questions/34551299/how-to-pass-ssl-verifypeer-in-rvest – Jacek Kotowski Apr 11 '17 at 18:38
  • You might want to consider updating your local system CAs. – hrbrmstr Apr 11 '17 at 20:13
  • It throws me an error in the case it does not find a table under a link. Then I would like it to continue and get next table if it can. How to skip this link? I tried with try() `map_df(1:nrow(full_df2), function(i) { pb$tick()$print() try(get_table(page_num = i),silent=T) } ) -> full_df3` does not help. It throws me an error `Error in bind_rows_(x, .id) : cannot convert object to a data frame` – Jacek Kotowski May 02 '17 at 15:27
  • I seemed to solve the issue in my comment by putting the html_table(tab)[[1]][,-c(1,8)] %>%.... part in the function get_table in a trycatch(, silent=T). I realise it is ugly. I am too ashamed to put it in your answer. Can you advise if it can print a comment: Warning, no table found this time, and then continue? – Jacek Kotowski May 04 '17 at 08:54