Estrarre notizie, ricette e altre informazioni dai siti web · 2015. 11. 15. · Estrarre notizie,...
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](https://reader036.fdocuments.net/reader036/viewer/2022071002/5fbefd8e88f699422e448991/html5/thumbnails/1.jpg)
![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](https://reader036.fdocuments.net/reader036/viewer/2022071002/5fbefd8e88f699422e448991/html5/thumbnails/2.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022071002/5fbefd8e88f699422e448991/html5/thumbnails/3.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022071002/5fbefd8e88f699422e448991/html5/thumbnails/4.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022071002/5fbefd8e88f699422e448991/html5/thumbnails/5.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022071002/5fbefd8e88f699422e448991/html5/thumbnails/6.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022071002/5fbefd8e88f699422e448991/html5/thumbnails/7.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022071002/5fbefd8e88f699422e448991/html5/thumbnails/8.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022071002/5fbefd8e88f699422e448991/html5/thumbnails/9.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022071002/5fbefd8e88f699422e448991/html5/thumbnails/10.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022071002/5fbefd8e88f699422e448991/html5/thumbnails/11.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022071002/5fbefd8e88f699422e448991/html5/thumbnails/12.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022071002/5fbefd8e88f699422e448991/html5/thumbnails/13.jpg)
RSS
blog Social media
podcast
notizie
![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](https://reader036.fdocuments.net/reader036/viewer/2022071002/5fbefd8e88f699422e448991/html5/thumbnails/14.jpg)
![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](https://reader036.fdocuments.net/reader036/viewer/2022071002/5fbefd8e88f699422e448991/html5/thumbnails/15.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022071002/5fbefd8e88f699422e448991/html5/thumbnails/16.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022071002/5fbefd8e88f699422e448991/html5/thumbnails/17.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022071002/5fbefd8e88f699422e448991/html5/thumbnails/18.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022071002/5fbefd8e88f699422e448991/html5/thumbnails/19.jpg)
Per programmare
Python → Linguaggio semplice da imparare
lxml → Modulo per elaborare HTML e XML
Requests → Modulo per salvare pagine
![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](https://reader036.fdocuments.net/reader036/viewer/2022071002/5fbefd8e88f699422e448991/html5/thumbnails/21.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022071002/5fbefd8e88f699422e448991/html5/thumbnails/22.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022071002/5fbefd8e88f699422e448991/html5/thumbnails/24.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022071002/5fbefd8e88f699422e448991/html5/thumbnails/25.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022071002/5fbefd8e88f699422e448991/html5/thumbnails/26.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022071002/5fbefd8e88f699422e448991/html5/thumbnails/27.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022071002/5fbefd8e88f699422e448991/html5/thumbnails/28.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022071002/5fbefd8e88f699422e448991/html5/thumbnails/29.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022071002/5fbefd8e88f699422e448991/html5/thumbnails/30.jpg)
![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](https://reader036.fdocuments.net/reader036/viewer/2022071002/5fbefd8e88f699422e448991/html5/thumbnails/31.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022071002/5fbefd8e88f699422e448991/html5/thumbnails/32.jpg)
![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](https://reader036.fdocuments.net/reader036/viewer/2022071002/5fbefd8e88f699422e448991/html5/thumbnails/33.jpg)
![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](https://reader036.fdocuments.net/reader036/viewer/2022071002/5fbefd8e88f699422e448991/html5/thumbnails/35.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022071002/5fbefd8e88f699422e448991/html5/thumbnails/36.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022071002/5fbefd8e88f699422e448991/html5/thumbnails/37.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022071002/5fbefd8e88f699422e448991/html5/thumbnails/38.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022071002/5fbefd8e88f699422e448991/html5/thumbnails/40.jpg)
![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](https://reader036.fdocuments.net/reader036/viewer/2022071002/5fbefd8e88f699422e448991/html5/thumbnails/41.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022071002/5fbefd8e88f699422e448991/html5/thumbnails/42.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022071002/5fbefd8e88f699422e448991/html5/thumbnails/43.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022071002/5fbefd8e88f699422e448991/html5/thumbnails/44.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022071002/5fbefd8e88f699422e448991/html5/thumbnails/45.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022071002/5fbefd8e88f699422e448991/html5/thumbnails/46.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022071002/5fbefd8e88f699422e448991/html5/thumbnails/47.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022071002/5fbefd8e88f699422e448991/html5/thumbnails/48.jpg)
![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](https://reader036.fdocuments.net/reader036/viewer/2022071002/5fbefd8e88f699422e448991/html5/thumbnails/49.jpg)
![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](https://reader036.fdocuments.net/reader036/viewer/2022071002/5fbefd8e88f699422e448991/html5/thumbnails/50.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022071002/5fbefd8e88f699422e448991/html5/thumbnails/51.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022071002/5fbefd8e88f699422e448991/html5/thumbnails/52.jpg)
?Domande finali