johnnyheineken johnnyheineken - 1 year ago 86
R Question

harvesting data via drop down list in R

I am trying to harvest data from this website
(it's in Czech)

I need to go through every possible combination of "Okres"(region) and "Obor"(specialization)

I tried rvest, but it does not seem to find that there is any dropdown list, html_form returns list of length 0.

therefore, as I am still a newbie in R, how can I "ask" the webpage to show me new combination of pages?

thank you


Answer Source

I'd use the following:


pg <- read_html("")

obor <- html_nodes(pg, "select[name='filterObor'] > option")
obor_df <- data_frame(
  value=xml_attr(obor, "value"),

## Observations: 115
## Variables: 2
## $ value  <chr> "", "16", "107", "17", "1", "19", "20", "21", "22", "29...
## $ option <chr> "", "alergologie a klinická imunologie", "algeziologie"...
okres <- html_nodes(pg, "select[name='filterOkresId'] > option")
okres_df <- data_frame(
  value=xml_attr(okres, "value"),

## Observations: 78
## Variables: 2
## $ value  <chr> "", "3201", "3202", "3701", "3702", "3703", "3801", "37...
## $ option <chr> "", "Benešov", "Beroun", "Blansko", "Brno-město", "Brno...

in case field order ever changes (plus it's good to get familiar with targeting nodes with CSS selectors and XPath selectors).

You still need to iterate over each pair (you can do that with nested purrr::map calls; I personally prbly wldn't use expand.grid or tidyr::complete for this).


You're going to have issues submitting the form with rvest since the site uses javacript to do some data processing before submitting.

You should use Chrome and open up Developer Tools to see what actually gets submitted field-wise and prbly switch to using httr::POST. If you have trouble with that, you should open up a new question on SO.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download