我的西班牙语有点生疏,但如果我没记错的话,您会尝试先切换los filtros de búsqueda por Sector e Institución,然后再切换sectorxinstitución 组合。
如果您单击其中一种组合,例如Aportaciones de Seguridad SocialxFondo 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,我们可以使用httr 在R 内发出完全相同的请求:
# 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这种重炮要简单得多。
此外,您还可以迭代 sectorxinstitución 组合。关键可能是更改 URL 参数以接收不同的组合(即 URL 的 ?query=... 部分。我自己没有对此进行调查,但是通过在请求其他组合时检查 DOM 和网络,您应该能够弄清楚这一点。
编辑 1:检查网络
在您的浏览器中,切换开发者工具并在里面点击标签网络。当您执行 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
所以看起来每个sectorxinstitución 都被编码为VW/XYZ。如果您检索所有这些,则可以迭代组合。
最后,如果您进一步检查网络,您会发现一些包含这些编码映射的请求。
编辑 2
正如怀疑的那样,在检查网络时,我使用以下请求 url https://nominatransparente.rhnet.gob.mx/assets/sectores.json 遇到了标记为 sectores.json 的请求。这至少包含我所指的sector 部分的映射。进一步观察可能会为instutución 产生类似的结果。
您可能必须切换并单击给定的sector,然后才能查看给定sector 的所有institucón 选项。然后在 DOM 中你会看到一个类似的映射。我建议:
1. Get the sector mapping
2. Find out inside the network how the list of instituciónes is given back. Probably something like:
-> Request containing sector-ID in the URL -> return a JSON with all instituciónes
3. Once you figure out the logic behind it, use httr::GET to create a list of all sector x institución
4. Once you have this list, iterate over all combinations to get JSON data as above.