Соскреб в Интернете с R, сообщение о том, что Javascript отключен - PullRequest
0 голосов
/ 31 октября 2018

Здравствуйте, я пытаюсь создать веб-страницу в R, и этот конкретный веб-сайт доставляет мне много хлопот. Я хочу извлечь таблицу отсюда: https://www.nationsreportcard.gov/profiles/stateprofile?chort=1&sub=MAT&sj=&sfj=NP&st=MN&year=2017

что я пробовал

Код:

url = 'https://www.nationsreportcard.gov/profiles/stateprofile?chort=1&sub=MAT&sj=&sfj=NP&st=MN&year=2017'

webpage = read_html(url)

data = webpage %>% html_nodes('p') %>% html_text()
data

Ouput:

[1] "\r\n            The page could not be loaded. This web site 
currently does not fully support browsers with \"JavaScript\" disabled. 
Please note that if you choose to continue without enabling 
\"JavaScript\" certain functionalities on this website may not be 
available.\r\n  

Ответы [ 2 ]

0 голосов
/ 01 ноября 2018

Введение сторонних зависимостей повышает сложность и затрудняет воспроизводимость.

Этот сайт использует XHR-запросы для асинхронной загрузки данных (и плохо IMO) после начальной загрузки страницы.

Откройте Инструменты разработчика в вашем браузере, затем загрузите страницу и перейдите в Сеть -> XHR:

enter image description here

Приложите немного усилий, чтобы получить настоящие, прекрасные данные JSON и использовать подверженный ошибкам анализ HTML-таблицы:

httr::GET(
  "https://www.nationsreportcard.gov/ndedataservice/ChartHandler.aspx?type=sp_state_map_datatable&subject=MAT&cohort=1&year=2017R3&_=2_0"
) -> res

str(xdat <- httr::content(res)$result, 2)
## List of 1
##  $ StateMap_DataTableData:List of 6
##   ..$ FocalJurisdiction: chr "NP"
##   ..$ Title            : chr "Mathematics, Grade 4<br />Difference in average scale scores between all jurisdictions and National public, for"| __truncated__
##   ..$ TableSortPrompt  : chr "Click on column headers to sort data by scores for a student group or score differences"
##   ..$ TableColumns     :List of 7
##   ..$ Statedata        :List of 54
##   ..$ Footnotes        :List of 4


dplyr::bind_rows(xdat$StateMap_DataTableData$Statedata)
## # A tibble: 54 x 11
##    Jurisdiction JurisdictionCode MN    SigDiff SigSymbol AB    AP    MN_FP
##    <chr>        <chr>            <chr> <chr>   <chr>     <chr> <chr> <chr>
##  1 Massachuset… MA               249   10      ▲         87    53    249.…
##  2 Minnesota    MN               249   10      ▲         86    53    248.…
##  3 DoDEA        DS               249   9       ▲         91    51    248.…
##  4 Virginia     VA               248   9       ▲         87    50    248.…
##  5 New Jersey   NJ               248   9       ▲         87    50    247.…
##  6 Wyoming      WY               248   9       ▲         89    51    247.…
##  7 Indiana      IN               247   7       ▲         86    48    246.…
##  8 Florida      FL               246   7       ▲         88    48    246.…
##  9 Nebraska     NE               246   6       ▲         85    49    245.…
## 10 New Hampshi… NH               245   6       ▲         85    48    245.…
## # ... with 44 more rows, and 3 more variables: SigDiff_FP <chr>,
## #   AB_FP <chr>, AP_FP <chr>

Вы можете выбрать ненужные столбцы и type.convert() или readr::type_convert(), чтобы получить правильные типы объектов.

Также рассмотрите возможность параметризации запроса GET для потенциального функционального использования; например, * 1 020 *

httr::GET(
  url = "https://www.nationsreportcard.gov/ndedataservice/ChartHandler.aspx",
  query = list(
    type = "sp_state_map_datatable",
    subject = "MAT",
    cohort = "1",
    year = "2017R3",
    `_` = "2_0"
  )
) -> res

^^ можно заключить в функцию с параметрами, переданными элементам списка query.

0 голосов
/ 31 октября 2018

В этом случае вы можете использовать RSelenium с docker для очистки веб-сайта Javascript

require("RSelenium")
require("rvest")
system('docker run -d -p 4445:4444 selenium/standalone-firefox')

remDr <-  RSelenium::remoteDriver(
  remoteServerAddr = "localhost",
  port = 4445L,
  browserName = "firefox"
)

#Start the remote driver
remDr$open()


url = 'https://www.nationsreportcard.gov/profiles/stateprofile? 
chort=1&sub=MAT&sj=&sfj=NP&st=MN&year=2017'

remDr$navigate(url)

doc <- read_html(remDr$getPageSource()[[1]])
table <- doc %>%
         html_nodes(xpath = '//*[@id="gridAvergeScore"]/table') %>%
         html_table(fill=TRUE)

head(table[[1]])

##    JURISDICTION AVERAGE SCORE (0 - 500)              AVERAGE SCORE (0 - 500) ACHIEVEMENT LEVEL PERCENTAGES ACHIEVEMENT LEVEL PERCENTAGES
## 1  JURISDICTION                   Score Difference from National public (NP)             At or above Basic        At or above Proficient
## 2 Massachusetts                     249                                   10                            87                            53
## 3     Minnesota                     249                                   10                            86                            53
## 4         DoDEA                     249                                    9                            91                            51
## 5      Virginia                     248                                    9                            87                            50
## 6    New Jersey                     248                                    9                            87                            50
...