我正在尝试使用RSelenium(与docker一起)从此网站提取数据:https://nominatransparente.rhnet.gob.mx
#-- Load package
library(RSelenium)
library(rvest)
library(xml2)
library(tidyverse)
#-- Remote driver
remDr <- remoteDriver(remoteServerAddr = "192.168.99.100", port = 4445L, browserName = "chrome")
remDr$open()
#-- navigate to the website
remDr$navigate("https://nominatransparente.rhnet.gob.mx/")
#-- confirm the website
remDr$getTitle()
#-- screenshot
remDr$screenshot(display = TRUE)
#-- Loading website's extra information
Sys.sleep(15)
#-- selecting filters: manipulate
webElement <- remDr$findElement("class name", "switch")
webElement$clickElement()
webElement <- remDr$findElement("class name", "ng-input")
webElement$clickElement()
直到这里,我仍然可以选择并单击下拉菜单,但是无法从下拉菜单中选择每个项目(我无法找到正确的xpath或ID)。我想浏览所有这些项目以及第二个下拉菜单,然后下载它们各自的CSV文件。
我想使用RSelenium执行所有操作。我见过类似的问题here,但使用rvest。有没有一种有效的方法来提取所有CSV文件?
[我的西班牙语有点生锈,但是如果我没记错的话,您尝试先切换los filtros de búsqueda por Sector e Institución
,然后进行sector
x institución
组合。
如果单击其中一个组合,例如Aportaciones de Seguridad Social
x Fondo de la Vivienda del ISSSTE
,则可以观察到以下网络请求:
method GET
url "https://dgti-ejz-mspadronserpub.200.34.175.120.nip.io/ms/InfoPadron/servidoresPublicosSector/19/HC6/1/100?query=nombres,primerApellido,segundoApellido,dependencia,tipoEntidad,nombrePuesto,sueldoBase,compensacionGarantizada"
Headers:
Host: dgti-ejz-mspadronserpub.200.34.175.120.nip.io
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:71.0) Gecko/20100101
Firefox/71.0
Accept: application/json
Accept-Language: de,en-US;q=0.7,en;q=0.3
Accept-Encoding: gzip, deflate, br
Referer: https://nominatransparente.rhnet.gob.mx/
Origin: https://nominatransparente.rhnet.gob.mx
Connection: keep-alive
TE: Trailers
此响应是一个包含相关数据的JSON
,我们可以使用R
在httr
中提出相同的请求:
# Make the request
headers <- c(
"Host" = "dgti-ejz-mspadronserpub.200.34.175.120.nip.io",
"User-Agent" = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv=71.0) Gecko/20100101 Firefox/71.0",
"Accept" = "application/json",
"Referer" = "https://nominatransparente.rhnet.gob.mx",
"Origin" = "https://nominatransparente.rhnet.gob.mx",
"Connection" = "keep-alive",
"TE" = "Trailers"
)
url <- "https://dgti-ejz-mspadronserpub.200.34.175.120.nip.io/ms/InfoPadron/servidoresPublicosSector/19/HC6/1/100?query=nombres,primerApellido,segundoApellido,dependencia,tipoEntidad,nombrePuesto,sueldoBase,compensacionGarantizada"
response <- httr::GET(url, httr::add_headers(headers))
# Extract the data
data <- httr::content(response)
# Example, the first entry
data$listDtoServidorPublico[[1]]
# $nombres
# [1] "JOSE OSCAR"
#
# $primerApellido
# [1] "ABURTO"
#
# $segundoApellido
# [1] "LOPEZ"
#
# $dependencia
# [1] "FONDO DE LA VIVIENDA DEL ISSSTE"
#
# $tipoEntidad
# [1] "ORGANISMO DESCENTRALIZADO"
#
# $nombrePuesto
# [1] "JEFE DE AREA PROF B EN PROC HIPOTEC FOVISSSTE"
#
# $sueldoBase
# [1] 9432
#
# $compensacionGarantizada
# [1] 2096
您可以看到,此版本比使用Selenium + Docker的重型火炮要简单得多。
此外,您可以迭代sector
x institución
组合。关键可能是更改URL参数以接收不同的组合(即URL的?query=...
部分。我本人并未对此进行调查,但是通过在请求其他组合时检查DOM和网络,您应该能够弄清楚这一点。
编辑:检查网络
在浏览器中,切换开发人员工具并在内部单击选项卡网络。当您执行Buscar时,将出现一个新请求,即一个与上述请求类似的请求(取决于所选组合)。
我已经完成了另一种组合,并观察到请求的URL是
https://dgti-ejz-mspadronserpub.200.34.175.120.nip.io/ms/InfoPadron/servidoresPublicosSector/25/C00/1/100?query=nombres,primerApellido,segundoApellido,dependencia,tipoEntidad,nombrePuesto,sueldoBase,compensacionGarantizada
因此,我必须调整网址的哪一部分是错误的:如果您比较这两个链接,这就是它们之间的不同之处
url_1 = x + 19/HC6 + y
url_2 = x + 25/C00 + y
# where
x = https://dgti-ejz-mspadronserpub.200.34.175.120.nip.io/ms/InfoPadron/servidoresPublicosSector/
y = /100?query=nombres,primerApellido,segundoApellido,dependencia,tipoEntidad,nombrePuesto,sueldoBase,compensacionGarantizada
因此看起来每个sector
x institución
都被编码为VW/XYZ
。如果检索到所有这些,则可以遍历组合。
最后,如果进一步检查网络机会,您会发现一些包含这些编码映射的请求。