SMI 2018 :: web scraping · Web data e web scraping Coleta de dados na web ... JSON ( JavaScript...

Preview:

Citation preview

SMI 2018 :: web scrapingparte I

Augusto Fadel

6 de novembro de 2018

parte I

R e RStudio

Web data e web scraping

Coleta de dados na web

·

·

·

download

API

web scraping

-

-

-

2/28

Web Scraping

web scraping

5/28

web data

Download

APIs (Application Programming Interfaces)

Web scraping

Crawler

·

·

·

·

6/28

web data

7/28

Download

tipos de arquivos

tipos de arquivoscsv (comma-separated values)

tsv (tab-separated values)

MS Excel (xls, xlsx)

zip

JSON (JavaScript Object Notation)

XML (Extensible Markup Language)

·

·

·

·

·

·

9/28

APIs

APIs

12/28

APIs

13/28

APIs

14/28

APIs

HTTP requestsGET

POST

DELETE

HEAD

outros

·

·

·

·

·

15/28

APIs

Respostas

Códigos de resposta HTTP

200: sucesso!

300: redirecionamento

400: erro de cliente

500: erro de servidor

·

·

·

·

16/28

APIs

The Star Wars API: swapi.co (pacote rwars) Sistema IBGE de Recuperação Automática: SIDRA Banco Central do Brasil: Portal de Dados Abertos

17/28

Web Scraping

web scraping

Estrutura HTML (tags)

<a href="http://www.ibge.gov.br/">IBGE</a>

título: <title> ... </title>

parágrafo de texto: <p> ... </p>

blocos: <div> ... </div>

tabela: <table> ... </table>

hiperlink (âncora): <a> ... </a>

·

·

·

·

·

19/28

web scraping

Extrair com rvest:

<a href="http://www.ibge.gov.br/">IBGE</a>

html_name()

html_attr()

html_text()

html_table()

·

·

·

·

20/28

web scraping

Amazon: amazon.com.br

21/28

web scraping

Selenium

Executar o servidor

Selenium 2: WebDriver

Standalone server (v3.9.1)

Pacote RSelenium

·

·

·

Usando Docker

Usando RSelenium::rsDriver()

Manualmente

·

·

·

22/28

web scraping

Funções RSelenium:

navigate()

goBack

goForward()

refresh()

findElement()

highlightElement()

clickElement()

mouseMoveToLocation()

click()

sendKeysToElement()

·

·

·

·

·

·

·

·

·

·

23/28

web scraping

GOL Linhas Aéreas: voegol.com.br

24/28

Boas práticas

boas práticas

Verificar e respeitar o robot.txt.

Identificação.

Usar httr::user_agent() com e-mail de contato.

Respeitar o limite de solicitações (rate-limiting).

Se não houver limite explícito, usar o bom senso.

Regra geral: intervalo de um segundo entre solicitações, usarSys.sleep(1).

Priorizar horários de menor tráfego.

·

·

·

·

·

·

·

26/28

Obrigado!

obrigado

Augusto Fadel

DPE/CEEC/GCAD

21 2142-0452

augusto.fadel@ibge.gov.br

augustofadel

28/28

Recommended