# install.packages('devtools') devtools::install_github('tomasbarcellos/valorrr')
library(valorrr) sessao <- html_session("http://www.valor.com.br") links <- sessao %>% links_pagina() noticias <- sessao %>% ler_noticia(links[1:10])
A resposta da função ler_noticia
é uma tibble
com 6 colunas:
i) url,
ii) data e hora em que a notícia foi publicada,
iii) título da notícia,
iv) autor da notícia,
v) notícia em si (texto),
vi) tags da notícia.
noticias
library(magrittr) noticias2 <- sessao %>% ler_noticia(links[11:20]) # 10 tags mais usadas no dia noticias2$tags %>% unlist() %>% table() %>% sort(decreasing = TRUE) %>% head(10)
library(dplyr) library(tidytext) library(tidyr) library(tm) tidy_noticias <- noticias2 %>% select(titulo, texto) %>% unnest_tokens(termo, texto, "ngrams", n = 2) stop_pt <- tibble(palavra = c(stopwords('pt-br'), "é")) noticias_limpo <- tidy_noticias %>% separate(termo, c("palavra1", "palavra2"), sep = " ") %>% filter(!palavra1 %in% stop_pt$palavra, !palavra2 %in% stop_pt$palavra) %>% mutate(termo = paste(palavra1, palavra2)) %>% select(-palavra1, -palavra2) # 10 termos mais comuns noticias_limpo %>% count(termo, sort = TRUE) # Nuvem de palavras library(wordcloud) minha_nuvem <- function(palavras, freq) { cores <- c('orange', 'darkblue', 'lightgray') wordcloud(palavras, freq, max.words = 50, random.color = TRUE, colors = cores) }
noticias_limpo %>% count(termo, sort = TRUE) %>% with(minha_nuvem(termo, n))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.