1
I’m trying to create a code where I can extract the amount of news from certain listed companies. But I’m having a lot of problems with the code. Especially with the loops.
library(RSelenium)
library(rvest)
url<- read_html("https://br.advfn.com/indice/ibovespa")
ibov2=html_text(html_nodes(url,".String.Column1 > a"))
ibov1=ibov2[-1]
ibov=ibov1[-1]
d2 <- data.frame(ibov)
write.table(d2,"ibovespa.csv",sep = ";", dec = ",",row.names = FALSE)
##NOMES DAS EMPRESAS NO IBOVESPA
d3 <- read.table("ibovespa.csv", sep = ";", dec = ",",header = TRUE)
names(d3)<- c("Empresas")
d4<-unique(d3)
d5<- data.frame(d4,row.names = c(1:61))
d6 <- as.character(d5[,])
remDr <- remoteDriver(remoteServerAddr = "192.168.99.100",
port = 4445L)
remDr$open()
remDr$refresh()
remDr$screenshot(display = TRUE)
remDr$navigate("http://valor.com.br/busca")
#Criar datas
trimestres <- seq(from= as.Date("2012-01-01"),to=as.Date("2018-12-30"),by ="3 months" )
emp <- data.frame()
numero_noticias <- data.frame()
for (i in d6){
emp<- append(emp,i)
#buscar empresas
buscar<-remDr$findElement(using = 'css selector',"#edit-search-theme-form-1")
buscar$sendKeysToElement(list(emp))
Sys.sleep(5)
clicar_buscar <- remDr$findElement(using= 'css selector',"#edit-submit")
clicar_buscar$clickElement()
for (i in seq_along(trimestres)) {
#selecionando datas
data_inicial<- as.Date(trimestres[i])
data_final<-trimestres[1+i]
if (data_final!=0) {
#inserindo primeira data
data_inicio <- remDr$findElement(using = 'css selector', "#edit-from-datepicker-popup")
data_inicio$sendKeysToElement(list(data_inicial))
Sys.sleep(10)
remDr$screenshot(display = TRUE)
#inserindo segunda data
data_final <- remDr$findElement(using = 'css selector', "#edit-to-datepicker-popup")
data_final$sendKeysToElement(list(data_final))
Sys.sleep(10)
#buscar
remDr$screenshot(display = TRUE)
clicar_ok <- remDr$findElement(using = 'css selector', "#busca-data-submit")
clicar_ok$clickElement()
Sys.sleep(10)
#extraindo numero de noticias
qnt_noticias <- remDr$findElement(using = 'css selector', "#destaques-ultimas-news > ul > li:nth-child(1) > p > a > span")
xyz<-qnt_noticias$getElementText()
numero_noticias <- append(numero_noticias,as.numeric(gsub("[()]", "", xyz)))
}
}
emp<-do.call(rbind, emp)
}
Whenever I try to run an error message appears. Like this: Selenium message:java.util.Hashmap cannot be cast to java.lang.String