El sitio web de la gira de la PGA tiene una página de clasificación y estoy tratando de raspar la tabla principal en el sitio web para un proyecto.
library(dplyr)
leaderboard_table <- xml2::read_html('https://www.pgatour.com/leaderboard.html') %>%
html_nodes('table') %>%
html_table()
sin embargo, en lugar de tirar de las tablas, devuelve este resultado extraño ...
Otras páginas, como la página de programación, se raspan bien sin ningún problema, consulte a continuación. Es solo la página de la tabla de clasificación con la que tengo problemas.
schedule_url <- 'https://www.pgatour.com/tournaments/schedule.html'
schedule_table <- xml2::read_html(schedule_url) %>% html_nodes('table.table-styled') %>% html_table()
schedule_df <- schedule_table[[1]]
# this works fine
Editar antes de la recompensa: la respuesta a continuación es un comienzo útil, sin embargo, hay un problema. El nombre de los archivos JSON cambia según la ronda ( /r/003
para la tercera ronda) y probablemente también en función de otros aspectos del torneo de golf. Actualmente hay esto que veo en la pestaña de elementos:
...however, using the leaderboard url link to the .json
file https://lbdata.pgatour.com/2021/r/005/leaderboard.json
is not helping... instead, I receive this error when using jsonlite::fromJson
Two questions then:
Is is possible to read this .JSON file into R? (perhaps it is protected in some way)? Maybe just an issue on my end, or am I missing something else in R here?
Given that the URL changes, how can I dynamically grab the URL value in R? It would be great if I could grab all of the global.leaderboardConfig
object somehow, because that would give me access to the leaderboardUrl
.
Thanks!!
As already mentioned, this page is dynamically generated by some javascript
.
Even the json
file address seems to be dynamic, and the address you're trying to open isn't valid anymore :
https://lbdata.pgatour.com/2021/r/003/leaderboard.json?userTrackingId=exp=1612495792~acl=*~hmac=722f704283f795e8121198427386ee075ce41e93d90f8979fd772b223ea11ab9
An error occurred while processing your request.
Reference #199.cf05d517.1613439313.4ed8cf21
Para obtener los datos, puede usar RSelenium después de instalar un servidor Docker Selenium .
La instalación es sencilla y Docker
está diseñada para hacer que las imágenes funcionen desde el primer momento.
Después de la Docker
instalación, ejecutar el Selenium
servidor es tan simple como:
docker run -d -p 4445:4444 selenium/standalone-firefox:2.53.0
Tenga en cuenta que esto en su conjunto requiere más 2 Gb
espacio en disco.
Selenium
emula un navegador web y permite, entre otros, obtener el HTML
contenido final de la página, después de renderizar javascript
:
library(RSelenium)
library(rvest)
remDr <- remoteDriver(
remoteServerAddr = "localhost",
port = 4445L,
browserName = "firefox"
)
# Open connexion to Selenium server
remDr$open()
remDr$getStatus()
remDr$navigate("https://www.pgatour.com/leaderboard.html")
players <- xml2::read_html(remDr$getPageSource()[[1]]) %>%
html_nodes(".player-name-col") %>%
html_text()
total <- xml2::read_html(remDr$getPageSource()[[1]]) %>%
html_nodes(".total") %>%
html_text()
data.frame(players = players, total = total[-1])
players total
1 Daniel Berger (PB) -18
2 Maverick McNealy (PB) -16
3 Patrick Cantlay (PB) -15
4 Jordan Spieth (PB) -15
5 Paul Casey (PB) -14
6 Nate Lashley (PB) -14
7 Charley Hoffman (PB) -13
8 Cameron Tringale (PB) -13
...
Como la tabla no usa la table
etiqueta, html_table
no funciona y las columnas deben extraerse individualmente.
Este artículo se recopila de Internet, indique la fuente cuando se vuelva a imprimir.
En caso de infracción, por favor [email protected] Eliminar
Déjame decir algunas palabras