簡體   English   中英

RS硒; 循環下載csv文件

[英]RSelenium; Looping and downloading csv files

我正在嘗試使用 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()

直到這里,我能夠 select 並單擊下拉菜單,但無法 select 下拉菜單中的每個項目(我無法找到正確的 Z3D788FA62D7C185A1BEE4C9147EE0) 我想通過這些項目中的每一個以及第二個下拉菜單中的 go,然后下載它們各自的 CSV 文件。

我想使用 RSelenium 執行所有操作。 我在這里看到了類似的問題,但使用了 rvest 有沒有一種有效的方法來提取所有 CSV 文件?

我的西班牙語有點生疏,但如果我沒記錯的話,您正在嘗試首先切換los filtros de búsqueda por Sector e Institución ,然后通過sector x institución組合切換 go。

如果您單擊其中一種組合,例如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 ,我們可以使用httrR中發出完全相同的請求:

# 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 參數以接收不同的組合(即?query=... URL 的一部分。我自己沒有對此進行調查,但是通過在請求其他組合時檢查 DOM 和網絡,您應該能夠弄清楚那出來。

編輯 1:檢查網絡

在您的瀏覽器中,切換開發人員工具並在內部單擊選項卡Network 當您執行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 的哪一部分,我錯了:如果您比較這兩個鏈接,那么它們的不同之處

 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 如果您檢索所有這些,則可以迭代這些組合。

最后,如果您進一步檢查網絡,您會發現一些包含這些編碼映射的請求。

編輯 2

As suspected, when checking the network I encoutered the request labelled sectores.json with the following request url https://nominatransparente.rhnet.gob.mx/assets/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.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM