Created
January 21, 2016 23:44
-
-
Save jtrecenti/4a4a7302add6736d1c76 to your computer and use it in GitHub Desktop.
empresas do brasil - scraper em R
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| library(rvest) | |
| library(httr) | |
| library(tidyr) | |
| library(dplyr) | |
| library(stringr) | |
| #' Tem captcha? | |
| #' | |
| #' Verifica se uma resposta tem captcha | |
| #' | |
| #' @param r resultado de uma request (pacote \code{\link{httr}}). | |
| #' | |
| #' @return \code{TRUE} se tiver captcha e \code{FALSE} caso contrário. | |
| tem_captcha <- function(r) { | |
| res <- r %>% | |
| content('text') %>% | |
| read_html() %>% | |
| html_nodes(xpath = '//form[@action="/verificarCaptcha/confirmar"]') %>% | |
| length() | |
| res > 0 | |
| } | |
| bloqueado <- function(r) { | |
| r %>% | |
| content('text') %>% | |
| str_detect('Acesso bloqueado') | |
| } | |
| #' Baixar categorias | |
| #' | |
| #' Baixa as categorias a partir do link inicial | |
| #' ex.: http://empresasdobrasil.com/empresas/alta-floresta-mt/ | |
| #' | |
| #' @param link URL do município. | |
| #' | |
| #' @return \code{data.frame} | |
| baixar_categorias <- function(link) { | |
| r <- GET(link) | |
| if (r$status_code != 200) return(data.frame(result = 'erro')) | |
| if (tem_captcha(r)) return(data.frame(result = 'captcha')) | |
| if (bloqueado(r)) return(data.frame(result = 'bloqueado')) | |
| u_base <- 'http://empresasdobrasil.com' | |
| r %>% | |
| content('text') %>% | |
| read_html() %>% | |
| html_nodes('.container a.linhas') %>% { | |
| data.frame(tipo = html_text(.), | |
| link_categoria = paste0(u_base, html_attr(., 'href')), | |
| stringsAsFactors = FALSE) | |
| } %>% | |
| mutate(result = 'OK') | |
| } | |
| #' Baixar empresas | |
| #' | |
| #' Baixa as empresas a partir do link de uma categoria | |
| #' ex.: http://empresasdobrasil.com/empresas/alta-floresta-mt/hoteis | |
| #' | |
| #' @param link URL da categoria | |
| #' | |
| #' @return \code{data.frame} | |
| baixar_empresas <- function(link) { | |
| r <- GET(link, write_disk('arq.html', overwrite = TRUE)) | |
| if (r$status_code != 200) return(data.frame(result = 'erro')) | |
| if (tem_captcha(r)) return(data.frame(result = 'captcha')) | |
| if (bloqueado(r)) return(data.frame(result = 'bloqueado')) | |
| u_base <- 'http://empresasdobrasil.com' | |
| r %>% | |
| content('text') %>% | |
| read_html() %>% | |
| html_node('table') %>% { | |
| tab <- html_table(.) %>% | |
| setNames('nome_razao') %>% | |
| separate(nome_razao, c('nome_fantasia', 'razao_social'), | |
| sep = ' - ', extra = 'merge', fill = 'left') | |
| links <- html_nodes(., 'a') %>% | |
| html_attr('href') | |
| tab$link_empresa <- paste0(u_base, links) | |
| tab | |
| } %>% | |
| mutate(result = 'OK') | |
| } | |
| #' Baixar infos de uma empresa | |
| #' | |
| #' Baixa as empresas a partir do link de uma categoria | |
| #' ex.: http://empresasdobrasil.com/empresas/alta-floresta-mt/hoteis | |
| #' | |
| #' @param link URL da categoria | |
| #' | |
| #' @return \code{data.frame} | |
| baixar_empresa <- function(link) { | |
| r <- GET(link) | |
| if (r$status_code != 200) return(data.frame(result = 'erro')) | |
| if (tem_captcha(r)) return(data.frame(result = 'captcha')) | |
| if (bloqueado(r)) return(data.frame(result = 'bloqueado')) | |
| r %>% | |
| content('text') %>% | |
| read_html() %>% { | |
| data.frame(titulo = html_text(html_nodes(., 'h4')), | |
| texto = html_text(html_nodes(., 'h5')), | |
| stringsAsFactors = FALSE) | |
| } %>% | |
| mutate(result = 'OK') | |
| } | |
| baixar_tudo <- function(link) { | |
| link <- 'http://empresasdobrasil.com/empresas/alta-floresta-mt/' | |
| d <- link %>% | |
| baixar_categorias() %>% | |
| group_by(tipo, link_categoria) %>% | |
| do(baixar_empresas(.$link_categoria)) %>% | |
| ungroup() %>% | |
| group_by(tipo, link_categoria, nome_fantasia, | |
| razao_social, link_empresa) %>% | |
| do(baixar_empresa(.$link_empresa)) | |
| d | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment