library(rvest)
library(tidyverse)
url_template <- "http://www.ema.europa.eu/ema/index.jsp?searchType=Name&applicationType=Initial+authorisation&applicationType=Post+authorisation&searchkwByEnter=false&mid=WC0b01ac058001d127&status=Positive&status=Negative&keyword=Enter+keywords&keyword=Enter+keywords&alreadyLoaded=true&curl=pages%%2Fmedicines%%2Flanding%%2Fsmop_search.jsp&startLetter=View+all&pageNo=%s"
获取页面的总#:
first <- sprintf(url_template, 1)
pg <- read_html(first)
html_nodes(pg, "div.pagination > ul > li:not([class])") %>%
tail(1) %>%
html_text(trim = TRUE) %>%
as.numeric() -> total_pages
只有3,但CLD很多在未来,所以建立一个进度条来招待你,并刮去表,然后提取链接并将其添加到表中:
pb <- progress_estimated(total_pages)
sprintf(url_template, 1:total_pages) %>%
map_df(function(URL) {
pb$tick()$print()
pg <- read_html(URL)
html_table(pg, trim = TRUE) %>%
.[[1]] %>%
set_names(c("name", "active_substance", "inn", "adopted", "outcome")) %>%
as_tibble() %>%
mutate(url = html_nodes(pg, "th[scope='row'] > a") %>% html_attr("href"))
}) -> pending_df
glimpse(pending_df)
## Observations: 67
## Variables: 6
## $ name <chr> "Lifmior", "Tamiflu", "Jylamvo", "Terrosa", "...
## $ active_substance <chr> "etanercept", "oseltamivir", "methotrexate", ...
## $ inn <chr> "etanercept", "oseltamivir", "methotrexate", ...
## $ adopted <chr> "2016-12-15", "2015-03-26", "2017-01-26", "20...
## $ outcome <chr> "Positive", "Positive", "Positive", "Positive...
## $ url <chr> "index.jsp?curl=pages/medicines/human/medicin...