Estrarre notizie, ricette e altre informazioni dai siti web · 2015. 11. 15. · Estrarre notizie,...

52

Transcript of Estrarre notizie, ricette e altre informazioni dai siti web · 2015. 11. 15. · Estrarre notizie,...

Page 1: Estrarre notizie, ricette e altre informazioni dai siti web · 2015. 11. 15. · Estrarre notizie, ricette e altre informazioni dai siti web Andrea Lazzarotto — andrealazzarotto.com
Page 2: Estrarre notizie, ricette e altre informazioni dai siti web · 2015. 11. 15. · Estrarre notizie, ricette e altre informazioni dai siti web Andrea Lazzarotto — andrealazzarotto.com

Estrarre notizie, ricette e altre informazioni dai siti web

Andrea Lazzarotto — andrealazzarotto.com

Page 3: Estrarre notizie, ricette e altre informazioni dai siti web · 2015. 11. 15. · Estrarre notizie, ricette e altre informazioni dai siti web Andrea Lazzarotto — andrealazzarotto.com

Domanda semplice...

«Posso avere un biscotto?»

Page 4: Estrarre notizie, ricette e altre informazioni dai siti web · 2015. 11. 15. · Estrarre notizie, ricette e altre informazioni dai siti web Andrea Lazzarotto — andrealazzarotto.com

Risposta deludente!

Page 5: Estrarre notizie, ricette e altre informazioni dai siti web · 2015. 11. 15. · Estrarre notizie, ricette e altre informazioni dai siti web Andrea Lazzarotto — andrealazzarotto.com

Pubblicità

Immagine

Contenuto

Articoli consigliati

Titolo

Commenti

Page 6: Estrarre notizie, ricette e altre informazioni dai siti web · 2015. 11. 15. · Estrarre notizie, ricette e altre informazioni dai siti web Andrea Lazzarotto — andrealazzarotto.com

Programma

Concetti di base

Strumenti utilizzati

Esempi pratici

Page 7: Estrarre notizie, ricette e altre informazioni dai siti web · 2015. 11. 15. · Estrarre notizie, ricette e altre informazioni dai siti web Andrea Lazzarotto — andrealazzarotto.com

Concetti di base

Page 8: Estrarre notizie, ricette e altre informazioni dai siti web · 2015. 11. 15. · Estrarre notizie, ricette e altre informazioni dai siti web Andrea Lazzarotto — andrealazzarotto.com

Web scraping

«La pratica di setacciare una pagina web per estrarre i dati richiesti nel formato più conveniente, preservandone la struttura.»— docs.python-guide.org

Page 9: Estrarre notizie, ricette e altre informazioni dai siti web · 2015. 11. 15. · Estrarre notizie, ricette e altre informazioni dai siti web Andrea Lazzarotto — andrealazzarotto.com

Pattern

Motivo/regolarità

Usato per estrarreo filtrare elementi

Esempio: h[1-6]

Page 10: Estrarre notizie, ricette e altre informazioni dai siti web · 2015. 11. 15. · Estrarre notizie, ricette e altre informazioni dai siti web Andrea Lazzarotto — andrealazzarotto.com

Semantica

Interesse → Ruolo e significato degli elementi

Titolo, elenco, tabella, …

Non riguarda l’aspetto grafico

Page 11: Estrarre notizie, ricette e altre informazioni dai siti web · 2015. 11. 15. · Estrarre notizie, ricette e altre informazioni dai siti web Andrea Lazzarotto — andrealazzarotto.com

HTML e XML

<ul class="amici"> <li>Tizio</li> <li>Caio</li></ul>

<persone gruppo="amici"> <nome>Tizio</nome> <nome>Caio</nome></persone>

Page 12: Estrarre notizie, ricette e altre informazioni dai siti web · 2015. 11. 15. · Estrarre notizie, ricette e altre informazioni dai siti web Andrea Lazzarotto — andrealazzarotto.com

Selettori CSS

Individuano elementi di interesse

Tabelle con classe “num”: table.num

Punti elenco in un oggettocon identificatore “main”: #main li

Page 13: Estrarre notizie, ricette e altre informazioni dai siti web · 2015. 11. 15. · Estrarre notizie, ricette e altre informazioni dai siti web Andrea Lazzarotto — andrealazzarotto.com

RSS

blog Social media

podcast

notizie

email

Page 14: Estrarre notizie, ricette e altre informazioni dai siti web · 2015. 11. 15. · Estrarre notizie, ricette e altre informazioni dai siti web Andrea Lazzarotto — andrealazzarotto.com
Page 15: Estrarre notizie, ricette e altre informazioni dai siti web · 2015. 11. 15. · Estrarre notizie, ricette e altre informazioni dai siti web Andrea Lazzarotto — andrealazzarotto.com

Strumenti utilizzati

Page 16: Estrarre notizie, ricette e altre informazioni dai siti web · 2015. 11. 15. · Estrarre notizie, ricette e altre informazioni dai siti web Andrea Lazzarotto — andrealazzarotto.com

Terminale (shell)

Page 17: Estrarre notizie, ricette e altre informazioni dai siti web · 2015. 11. 15. · Estrarre notizie, ricette e altre informazioni dai siti web Andrea Lazzarotto — andrealazzarotto.com

pasticciere | barista | Pinco | Pallina

La pipe è un “passaggio”, come una compravendita

Page 18: Estrarre notizie, ricette e altre informazioni dai siti web · 2015. 11. 15. · Estrarre notizie, ricette e altre informazioni dai siti web Andrea Lazzarotto — andrealazzarotto.com

Pandoc

Convertitore di documenti testuali

HTML, Markdown, OpenDocument, ...

Page 19: Estrarre notizie, ricette e altre informazioni dai siti web · 2015. 11. 15. · Estrarre notizie, ricette e altre informazioni dai siti web Andrea Lazzarotto — andrealazzarotto.com

Per programmare

Python → Linguaggio semplice da imparare

lxml → Modulo per elaborare HTML e XML

Requests → Modulo per salvare pagine

Page 20: Estrarre notizie, ricette e altre informazioni dai siti web · 2015. 11. 15. · Estrarre notizie, ricette e altre informazioni dai siti web Andrea Lazzarotto — andrealazzarotto.com

Feed43

«Questo servizio converte qualsiasi pagina web in RSS.»— feed43.com

Page 21: Estrarre notizie, ricette e altre informazioni dai siti web · 2015. 11. 15. · Estrarre notizie, ricette e altre informazioni dai siti web Andrea Lazzarotto — andrealazzarotto.com

Ultimi ritocchi

Estrarre articoli interi dagli RSS: feedex.net fulltextrssfeed.com wizardrss.com

Filtrare i contenuti: feedsifter.com

Page 22: Estrarre notizie, ricette e altre informazioni dai siti web · 2015. 11. 15. · Estrarre notizie, ricette e altre informazioni dai siti web Andrea Lazzarotto — andrealazzarotto.com

Esempi pratici

Page 24: Estrarre notizie, ricette e altre informazioni dai siti web · 2015. 11. 15. · Estrarre notizie, ricette e altre informazioni dai siti web Andrea Lazzarotto — andrealazzarotto.com

Inizio e fine del contenuto

<div class="recepy hrecipe"> <header> <h1 class="fn">Cupcake mimosa</h1> <a class="rkat" href="/ricette-cat/Dolci-e-Desserts/" title="Ricette della categoria Dolci e Desserts">Dolci e Desserts</a> </header> <div class="top-adsense">

... f ino al momento di gustarli!</p><div class="tools"> <a target="new" class="pdf" href="/images/PDF/GZRic-Cupcake-mimosa.pdf"> <span class="icon"></span> Scarica la <br>versione PDF</a>

Page 25: Estrarre notizie, ricette e altre informazioni dai siti web · 2015. 11. 15. · Estrarre notizie, ricette e altre informazioni dai siti web Andrea Lazzarotto — andrealazzarotto.com

Conversione in Markdown

Linguaggio semplice per testi strutturati

Usiamo il comando:

curl http://ricette.giallozaf ferano.it/Cupcake-mimosa.html | pandoc -f html -t markdown

Page 26: Estrarre notizie, ricette e altre informazioni dai siti web · 2015. 11. 15. · Estrarre notizie, ricette e altre informazioni dai siti web Andrea Lazzarotto — andrealazzarotto.com

Risultato

<div class="recepy hrecipe">

Cupcake mimosa {.fn}==============

[Dolci e Desserts](/ricette-cat/Dolci-e-Desserts/ "Ricette della categoria Dolci ...<div class="top-adsense">

f ino al momento di gustarli!

<div class="tools">

[<span class="icon"></span> Scarica la\versione PDF](/images/PDF/GZRic-Cupcake-mimosa.pdf)

Page 27: Estrarre notizie, ricette e altre informazioni dai siti web · 2015. 11. 15. · Estrarre notizie, ricette e altre informazioni dai siti web Andrea Lazzarotto — andrealazzarotto.com

Ritagliamo!

Vicino al titolo → {.fn}

Dopo la ricetta → <div class="tools">

Page 28: Estrarre notizie, ricette e altre informazioni dai siti web · 2015. 11. 15. · Estrarre notizie, ricette e altre informazioni dai siti web Andrea Lazzarotto — andrealazzarotto.com

Quindi...

Righe a partire da “{.fn}”

Fino a “class=tools”

Togli l’ultima riga

grep -A 1000000 "{.fn}"

grep -B 1000000 "class=\"tools"

head -n -1

Page 29: Estrarre notizie, ricette e altre informazioni dai siti web · 2015. 11. 15. · Estrarre notizie, ricette e altre informazioni dai siti web Andrea Lazzarotto — andrealazzarotto.com

Markdown → ODT

Con i comandi precedenti, in cascata, otteniamo a video la ricetta in Markdown (filtrata):

curl http://ricette.giallozaf ferano.it/Cupcake-mimosa.html | pandoc -f html -t markdown | grep -A 1000000 "{.fn}" | grep -B 1000000 "class=\"tools" | head -n -1

Finiamo con: | pandoc -f markdown -o ricetta.odt

Page 30: Estrarre notizie, ricette e altre informazioni dai siti web · 2015. 11. 15. · Estrarre notizie, ricette e altre informazioni dai siti web Andrea Lazzarotto — andrealazzarotto.com
Page 31: Estrarre notizie, ricette e altre informazioni dai siti web · 2015. 11. 15. · Estrarre notizie, ricette e altre informazioni dai siti web Andrea Lazzarotto — andrealazzarotto.com

Conversione in PDF

LaTeX → PDF di alta qualità

Caratteri speciali → meglio usare XeLaTeX

| pandoc -f markdown --latex-engine=xelatex -o ricetta.pdf

Page 32: Estrarre notizie, ricette e altre informazioni dai siti web · 2015. 11. 15. · Estrarre notizie, ricette e altre informazioni dai siti web Andrea Lazzarotto — andrealazzarotto.com
Page 33: Estrarre notizie, ricette e altre informazioni dai siti web · 2015. 11. 15. · Estrarre notizie, ricette e altre informazioni dai siti web Andrea Lazzarotto — andrealazzarotto.com
Page 34: Estrarre notizie, ricette e altre informazioni dai siti web · 2015. 11. 15. · Estrarre notizie, ricette e altre informazioni dai siti web Andrea Lazzarotto — andrealazzarotto.com

grappalug.org

Page 35: Estrarre notizie, ricette e altre informazioni dai siti web · 2015. 11. 15. · Estrarre notizie, ricette e altre informazioni dai siti web Andrea Lazzarotto — andrealazzarotto.com

Inizio di ogni talk

<div id="post-633" class="hcalendar post post-633event type-event status-publish">

<div class="post-header">

<h2 class="post-title"> <a href="http://grappalug.org/eventi/evento/postgresql-val-bene-una-grappa/" rel="bookmark" title="PostgreSQL val bene una Grappa!">PostgreSQL val bene una Grappa!</a></h2>

<div class="post-meta">

Page 36: Estrarre notizie, ricette e altre informazioni dai siti web · 2015. 11. 15. · Estrarre notizie, ricette e altre informazioni dai siti web Andrea Lazzarotto — andrealazzarotto.com

Acquisizione pagina

# Codice Python

from lxml import htmlimport requests

cont = requests.get("http://grappalug.org/eventi/").textalbero = html.fromstring(cont)

Page 37: Estrarre notizie, ricette e altre informazioni dai siti web · 2015. 11. 15. · Estrarre notizie, ricette e altre informazioni dai siti web Andrea Lazzarotto — andrealazzarotto.com

Titoli e URL

tags = [ (el.text.strip(), el.attrib['href']) for el in albero.cssselect('div.post h2 > a')]

for t in tags: print ','.join(t)

Page 38: Estrarre notizie, ricette e altre informazioni dai siti web · 2015. 11. 15. · Estrarre notizie, ricette e altre informazioni dai siti web Andrea Lazzarotto — andrealazzarotto.com

CSV → LibreOffice

Page 40: Estrarre notizie, ricette e altre informazioni dai siti web · 2015. 11. 15. · Estrarre notizie, ricette e altre informazioni dai siti web Andrea Lazzarotto — andrealazzarotto.com
Page 41: Estrarre notizie, ricette e altre informazioni dai siti web · 2015. 11. 15. · Estrarre notizie, ricette e altre informazioni dai siti web Andrea Lazzarotto — andrealazzarotto.com

Caricamento pagina

Page 42: Estrarre notizie, ricette e altre informazioni dai siti web · 2015. 11. 15. · Estrarre notizie, ricette e altre informazioni dai siti web Andrea Lazzarotto — andrealazzarotto.com

Primo pattern

Page 43: Estrarre notizie, ricette e altre informazioni dai siti web · 2015. 11. 15. · Estrarre notizie, ricette e altre informazioni dai siti web Andrea Lazzarotto — andrealazzarotto.com

Risultati

Page 44: Estrarre notizie, ricette e altre informazioni dai siti web · 2015. 11. 15. · Estrarre notizie, ricette e altre informazioni dai siti web Andrea Lazzarotto — andrealazzarotto.com

Pattern raffinato

Page 45: Estrarre notizie, ricette e altre informazioni dai siti web · 2015. 11. 15. · Estrarre notizie, ricette e altre informazioni dai siti web Andrea Lazzarotto — andrealazzarotto.com

Risultati

Page 46: Estrarre notizie, ricette e altre informazioni dai siti web · 2015. 11. 15. · Estrarre notizie, ricette e altre informazioni dai siti web Andrea Lazzarotto — andrealazzarotto.com

Composizione RSS

Page 47: Estrarre notizie, ricette e altre informazioni dai siti web · 2015. 11. 15. · Estrarre notizie, ricette e altre informazioni dai siti web Andrea Lazzarotto — andrealazzarotto.com

Anteprima

Page 48: Estrarre notizie, ricette e altre informazioni dai siti web · 2015. 11. 15. · Estrarre notizie, ricette e altre informazioni dai siti web Andrea Lazzarotto — andrealazzarotto.com
Page 49: Estrarre notizie, ricette e altre informazioni dai siti web · 2015. 11. 15. · Estrarre notizie, ricette e altre informazioni dai siti web Andrea Lazzarotto — andrealazzarotto.com
Page 50: Estrarre notizie, ricette e altre informazioni dai siti web · 2015. 11. 15. · Estrarre notizie, ricette e altre informazioni dai siti web Andrea Lazzarotto — andrealazzarotto.com

Un’ultima cosa...

Page 51: Estrarre notizie, ricette e altre informazioni dai siti web · 2015. 11. 15. · Estrarre notizie, ricette e altre informazioni dai siti web Andrea Lazzarotto — andrealazzarotto.com

Approfondimenti

PHP → Scraping e output in RSS

Documenti PDF: OCR con Tesseract Input per Pandoc con pdf2html

Espressioni regolari → filtri avanzati

Page 52: Estrarre notizie, ricette e altre informazioni dai siti web · 2015. 11. 15. · Estrarre notizie, ricette e altre informazioni dai siti web Andrea Lazzarotto — andrealazzarotto.com

?Domande finali