动态列表的 RSelenium 问题(循环不工作)

问题描述 投票:0回答:0

我正在尝试访问此网站“https://www.almg.gov.br/atividade-parlamentar/projetos-de-lei/texto/?tipo=PL&num=173&ano=2023”,并删除女巫的描述法律,问题是描述在该列表的链接中。我正在尝试使用 Rselenium,但我可以打开列表中的女巫链接,但他只接受第一个或最后一个描述。

那是我使用的代码:

`#Puxando os Pacotes
library(RSelenium)
library(tidyverse)
library(netstat)
library(rvest)
library(data.table)
library(dplyr)

#Inicializando o Selenium 
remote_driver <- rsDriver(browser = 'firefox',
                          verbose = FALSE,
                          port = free_port(), chromever = NULL)

remDr <- remote_driver$client
remDr$open()
link <-remDr$navigate('https://www.almg.gov.br/atividade-parlamentar/projetos-de-lei/texto/?tipo=PL&num=173&ano=2023')

#IDENTIFICAÇÕES

identificação <- remDr$findElements(using = "css", ".js_interpretarLinksREADY") 
numero_data <-lapply(identificação, function(x) {
  x$getElementText() %>% unlist()
}) %>% flatten_chr()

identificação2 <-remDr$findElements(using = "xpath", "/html/body/main/div[4]/div/span/p[4]/a")
link_lei <- lapply(identificação, function(x) {
  x$getElementAttribute('href') %>% unlist()
}) %>% flatten_chr()


#ABRINDO UMA LEI 
remDr$navigate("https://www.almg.gov.br/legislacao-mineira/LEI/102/1947/")
ementa <- remDr$findElement(using = "xpath", "/html/body/main/div[4]/div/div[2]/div[1]")$getElementText() %>%
  unlist()



#RETORNANDO À PAGINA PRINCIPAL
remDr$navigate('https://www.almg.gov.br/atividade-parlamentar/projetos-de-lei/texto/?tipo=PL&num=173&ano=2023')


#PARA CAPTAR AS DESCRIÇÕES

for (t_url in link_lei [1:3]) {
  remDr$navigate(t_url)
  descrição <- ementa
  remDr$navigate('https://www.almg.gov.br/atividade-parlamentar/projetos-de-lei/texto/?tipo=PL&num=173&ano=2023')
  Sys.sleep( 1 )
}`
loops for-loop xpath rstudio rselenium
© www.soinside.com 2019 - 2024. All rights reserved.