Skip to content

Instantly share code, notes, and snippets.

@jjesusfilho
Last active February 26, 2021 17:27
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jjesusfilho/3b0f99f80a0bce019bda3fce54486b9d to your computer and use it in GitHub Desktop.
Save jjesusfilho/3b0f99f80a0bce019bda3fce54486b9d to your computer and use it in GitHub Desktop.
ssp sp scraper
# Orientação
#
# Para selecionar os crimes, use os seguintes nomes:
#
# Para Taxa de homicídio, use: "TaxaHomicidio"
# Para Homicídio doloso, use: "Homicicio" *** Atenção, é "Homicicio" e não "Homicidio"***
# Para Latrocínio, use: "Latrocinio"
# Para Lesão corporal seguida de morte, use: "LesaoMorte"
# Para registro de óbitos - IML, use: "IML",
# Para Morte decorrente de ação poicial, use: "MortePolicial"
# Para Morte suspeita, use: "MorteSuspeita"
# Para Furto de veículo, use: "FurtoVeiculo"
# Para Roubo de veículo, use: "RouboVeiculo"
# Para Furto de celular, use: "FurtoCelular"
# Para Roubo de celular, use: "RouboCelular"
#
# Para o ano, use dois ou quatro dígitos, ano=2017 ou ano=17
#
# Para o mês, use o número. Por exemplo, para o mês de fevereiro, use: mes=2
library(httr)
library(xml2)
library(rvest)
library(dplyr)
library(stringr)
crimes_ssp_sp<-function(crime,ano,mes){
ano<-str_extract(ano,"\\d{2}$")
url<-"http://www.ssp.sp.gov.br/transparenciassp/"
s<-url %>%
GET() %>%
content()
`__EVENTVALIDATION`<- s %>%
xml_find_all("//input[2]") %>%
xml_attr("value")
`__VIEWSTATE`<- s %>%
xml_find_all("//input[1]") %>%
xml_attr("value") %>%
.[1]
body<-list(`__EVENTTARGET`="",
`__VIEWSTATE`= `__VIEWSTATE`,
`__EVENTVALIDATION`=`__EVENTVALIDATION`,
`ctl00$cphBody$hdfExport`="")
body[[1]]<-paste0("ctl00$cphBody$btn",crime)
s1<-url %>%
POST(body=body) %>%
content()
`__EVENTVALIDATION`<-s1 %>%
xml_find_all("//input[2]") %>%
xml_attr("value")
`__VIEWSTATE`<-s1 %>%
xml_find_all("//input[1]") %>%
xml_attr("value") %>%
.[1]
body2<-list(`__EVENTTARGET`="",
`__EVENTARGUMENT`="",
`__LASTFOCUS`="",
`__VIEWSTATE`= `__VIEWSTATE`,
`__EVENTVALIDATION`=`__EVENTVALIDATION`,
`ctl00$cphBody$filtroDepartamento`=0,
`ctl00$cphBody$hdfExport`="")
body2[[1]]<-ano %>%
paste0("ctl00$cphBody$lkAno",.)
s3<-url %>%
POST(body=body2) %>%
content()
body2[[1]]<-mes %>%
paste0("ctl00$cphBody$lkMes",.)
s3<-url %>%
POST(body=body2) %>%
content()
tb<-s3 %>%
html_table(fill=T) %>%
.[[1]] %>%
head(-2)
paginas<-s3 %>%
xml_find_all("//*[@colspan='7']//a") %>%
xml_text()
body2[[1]]<-"ctl00$cphBody$grdListBO"
for(i in seq_along(paginas)){
body2[[2]]<-paste0("Page$",paginas[i])
tb1<- url %>%
POST(body=body2) %>%
content() %>%
html_table(fill=T) %>%
.[[1]] %>%
head(-2)
tb<-bind_rows(tb,tb1)
}
return(tb)
}
@aquilescs
Copy link

aquilescs commented Feb 28, 2019

Este código ainda funciona ai?
Aqui não consigo fazer ele rodar. Só me retorna uma tabela vazia.
Chamei a função assim, após usar sem o nome das variáveis (crime, ano e mês). Em ambos os casos o resultado foi o mesmo.

crimes_ssp_sp(crime='FurtoCelular',ano=18,mes=11)

@jjesusfilho
Copy link
Author

Este código ainda funciona ai?
Aqui não consigo fazer ele rodar. Só me retorna uma tabela vazia.
Chamei a função assim, após usar sem o nome das variáveis (crime, ano e mês). Em ambos os casos o resultado foi o mesmo.

crimes_ssp_sp(crime='FurtoCelular',ano=18,mes=11)

Não funciona.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment