Upgrade to Pro — share decks privately, control downloads, hide ads and more …

INTRODUÇÃO AO XPATH: NAVEGANDO (E EXTRAINDO DADOS) DE DOCUMENTOS HTML

INTRODUÇÃO AO XPATH: NAVEGANDO (E EXTRAINDO DADOS) DE DOCUMENTOS HTML

Uma breve introdução da utilização do XPath para a extração de informações de arquivos HTML.

Apresentado em 09/06/2019 na edição 2019 do Caipyra (https://caipyra.python.org.br/), uma conferência Python regional no interior de SP.

Renne Rocha

June 09, 2019
Tweet

More Decks by Renne Rocha

Other Decks in Programming

Transcript

  1. 6/10/2019 Introdução ao XPath file:///home/renne/projects/rennerocha/apresentacoes/20190609_CaipyraXPath/index.html?print-pdf 2/50 RENNE ROCHA RENNE ROCHA

    Python Developer na Scrapinghub Laboratório Hacker de Campinas (https://lhc.net.br) Grupy-Campinas | Grupy-Jundiaí [email protected] @rennerocha (Github, Twitter, Telegram, etc)
  2. 6/10/2019 Introdução ao XPath file:///home/renne/projects/rennerocha/apresentacoes/20190609_CaipyraXPath/index.html?print-pdf 8/50 XML (Extensible Markup Language)

    é um formato baseado em texto para representação de informações estruturadas, como por exemplo documentos, con gurações, livros, transações, etc.
  3. 6/10/2019 Introdução ao XPath file:///home/renne/projects/rennerocha/apresentacoes/20190609_CaipyraXPath/index.html?print-pdf 9/50 XML (Extensible Markup Language)

    é um formato baseado em texto para representação de informações estruturadas, como por exemplo documentos, con gurações, livros, transações, etc.
  4. 6/10/2019 Introdução ao XPath file:///home/renne/projects/rennerocha/apresentacoes/20190609_CaipyraXPath/index.html?print-pdf 11/50 CASOS DE USO CASOS

    DE USO Arquivos de Con guração Troca de mensagens entre aplicações Padrão para documentos (Open Document Format)
  5. 6/10/2019 Introdução ao XPath file:///home/renne/projects/rennerocha/apresentacoes/20190609_CaipyraXPath/index.html?print-pdf 18/50 from parsel import Selector

    with open("brigadeiro.html", "r") as xml_file: content = xml_file.read() selector = Selector(text=content) 1 2 3 4 5
  6. 6/10/2019 Introdução ao XPath file:///home/renne/projects/rennerocha/apresentacoes/20190609_CaipyraXPath/index.html?print-pdf 20/50 <html> 1 <head> 2

    <title>Receita de Brigadeiro</title> 3 </head> 4 <body data-content="receita"> 5 <h1>Ingredientes</h1> 6 <ul> 7 <li data-qtd="1" data-unit="colher">Manteiga</li> 8 <li data-qtd="1" data-unit="lata">Leite Condensado</li> 9 <li data-qtd="4" data-unit="colher">Chocolate em Pó</li> 10 <li data-qtd="1" data-unit="pacote">Choc. Granulado</li> 11 </ul> 12 <h1>Instruções de Preparo</h1> 13 <ol> 14 <li>Aqueça a panela em fogo médio.</li> 15 <li>Acrescente 1 colher de sopa de manteiga.</li> 16 <li>Acrescente o Leite Condensado</li> 17 <li>Acrescente o Chocolate em Pó</li> 18 <li>Mexa sem parar até desgrudar da panela.</li> 19 </ol> 20 </body> 21 </html> 22
  7. 6/10/2019 Introdução ao XPath file:///home/renne/projects/rennerocha/apresentacoes/20190609_CaipyraXPath/index.html?print-pdf 22/50 <title>Receita de Brigadeiro</title> <html>

    1 <head> 2 3 </head> 4 <body data-content="receita"> 5 <h1>Ingredientes</h1> 6 <ul> 7 <li data-qtd="1" data-unit="colher">Manteiga</li> 8 <li data-qtd="1" data-unit="lata">Leite Condensado</li> 9 <li data-qtd="4" data-unit="colher">Chocolate em Pó</li> 10 <li data-qtd="1" data-unit="pacote">Choc. Granulado</li> 11 </ul> 12 <h1>Instruções de Preparo</h1> 13 <ol> 14 <li>Aqueça a panela em fogo médio.</li> 15 <li>Acrescente 1 colher de sopa de manteiga.</li> 16 <li>Acrescente o Leite Condensado</li> 17 <li>Acrescente o Chocolate em Pó</li> 18 <li>Mexa sem parar até desgrudar da panela.</li> 19 </ol> 20 </body> 21 </html> 22
  8. 6/10/2019 Introdução ao XPath file:///home/renne/projects/rennerocha/apresentacoes/20190609_CaipyraXPath/index.html?print-pdf 24/50 <title>Receita de Brigadeiro</title> <html>

    1 <head> 2 3 </head> 4 <body data-content="receita"> 5 <h1>Ingredientes</h1> 6 <ul> 7 <li data-qtd="1" data-unit="colher">Manteiga</li> 8 <li data-qtd="1" data-unit="lata">Leite Condensado</li> 9 <li data-qtd="4" data-unit="colher">Chocolate em Pó</li> 10 <li data-qtd="1" data-unit="pacote">Choc. Granulado</li> 11 </ul> 12 <h1>Instruções de Preparo</h1> 13 <ol> 14 <li>Aqueça a panela em fogo médio.</li> 15 <li>Acrescente 1 colher de sopa de manteiga.</li> 16 <li>Acrescente o Leite Condensado</li> 17 <li>Acrescente o Chocolate em Pó</li> 18 <li>Mexa sem parar até desgrudar da panela.</li> 19 </ol> 20 </body> 21 </html> 22
  9. 6/10/2019 Introdução ao XPath file:///home/renne/projects/rennerocha/apresentacoes/20190609_CaipyraXPath/index.html?print-pdf 26/50 <h1>Ingredientes</h1> <h1>Instruções de Preparo</h1>

    <html> 1 <head> 2 <title>Receita de Brigadeiro</title> 3 </head> 4 <body data-content="receita"> 5 6 <ul> 7 <li data-qtd="1" data-unit="colher">Manteiga</li> 8 <li data-qtd="1" data-unit="lata">Leite Condensado</li> 9 <li data-qtd="4" data-unit="colher">Chocolate em Pó</li> 10 <li data-qtd="1" data-unit="pacote">Choc. Granulado</li> 11 </ul> 12 13 <ol> 14 <li>Aqueça a panela em fogo médio.</li> 15 <li>Acrescente 1 colher de sopa de manteiga.</li> 16 <li>Acrescente o Leite Condensado</li> 17 <li>Acrescente o Chocolate em Pó</li> 18 <li>Mexa sem parar até desgrudar da panela.</li> 19 </ol> 20 </body> 21 </html> 22
  10. 6/10/2019 Introdução ao XPath file:///home/renne/projects/rennerocha/apresentacoes/20190609_CaipyraXPath/index.html?print-pdf 28/50 <li data-qtd="1" data-unit="colher">Manteiga</li> <li

    data-qtd="1" data-unit="lata">Leite Condensado</li> <li data-qtd="4" data-unit="colher">Chocolate em Pó</li> <li data-qtd="1" data-unit="pacote">Choc. Granulado</li> <html> 1 <head> 2 <title>Receita de Brigadeiro</title> 3 </head> 4 <body data-content="receita"> 5 <h1>Ingredientes</h1> 6 <ul> 7 8 9 10 11 </ul> 12 <h1>Instruções de Preparo</h1> 13 <ol> 14 <li>Aqueça a panela em fogo médio.</li> 15 <li>Acrescente 1 colher de sopa de manteiga.</li> 16 <li>Acrescente o Leite Condensado</li> 17 <li>Acrescente o Chocolate em Pó</li> 18 <li>Mexa sem parar até desgrudar da panela.</li> 19 </ol> 20 </body> 21 </html> 22
  11. 6/10/2019 Introdução ao XPath file:///home/renne/projects/rennerocha/apresentacoes/20190609_CaipyraXPath/index.html?print-pdf 30/50 <li data-qtd="1" data-unit="lata">Leite Condensado</li>

    <html> 1 <head> 2 <title>Receita de Brigadeiro</title> 3 </head> 4 <body data-content="receita"> 5 <h1>Ingredientes</h1> 6 <ul> 7 <li data-qtd="1" data-unit="colher">Manteiga</li> 8 9 <li data-qtd="4" data-unit="colher">Chocolate em Pó</li> 10 <li data-qtd="1" data-unit="pacote">Choc. Granulado</li> 11 </ul> 12 <h1>Instruções de Preparo</h1> 13 <ol> 14 <li>Aqueça a panela em fogo médio.</li> 15 <li>Acrescente 1 colher de sopa de manteiga.</li> 16 <li>Acrescente o Leite Condensado</li> 17 <li>Acrescente o Chocolate em Pó</li> 18 <li>Mexa sem parar até desgrudar da panela.</li> 19 </ol> 20 </body> 21 </html> 22
  12. 6/10/2019 Introdução ao XPath file:///home/renne/projects/rennerocha/apresentacoes/20190609_CaipyraXPath/index.html?print-pdf 32/50 <html> <head> <title>Receita de

    Brigadeiro</title> </head> <body data-content="receita"> <h1>Ingredientes</h1> <ul> <li data-qtd="1" data-unit="colher">Manteiga</li> <li data-qtd="1" data-unit="lata">Leite Condensado</li> <li data-qtd="4" data-unit="colher">Chocolate em Pó</li> <li data-qtd="1" data-unit="pacote">Choc. Granulado</li> </ul> <h1>Instruções de Preparo</h1> <ol> <li>Aqueça a panela em fogo médio.</li> <li>Acrescente 1 colher de sopa de manteiga.</li> <li>Acrescente o Leite Condensado</li> <li>Acrescente o Chocolate em Pó</li> <li>Mexa sem parar até desgrudar da panela.</li> </ol> </body> </html>
  13. 6/10/2019 Introdução ao XPath file:///home/renne/projects/rennerocha/apresentacoes/20190609_CaipyraXPath/index.html?print-pdf 35/50 <li data-qtd="4" data-unit="colher">Chocolate em

    Pó</li> <li>Acrescente o Chocolate em Pó</li> <html> 1 <head> 2 <title>Receita de Brigadeiro</title> 3 </head> 4 <body data-content="receita"> 5 <h1>Ingredientes</h1> 6 <ul> 7 <li data-qtd="1" data-unit="colher">Manteiga</li> 8 <li data-qtd="1" data-unit="lata">Leite Condensado</li> 9 10 <li data-qtd="1" data-unit="pacote">Choc. Granulado</li> 11 </ul> 12 <h1>Instruções de Preparo</h1> 13 <ol> 14 <li>Aqueça a panela em fogo médio.</li> 15 <li>Acrescente 1 colher de sopa de manteiga.</li> 16 <li>Acrescente o Leite Condensado</li> 17 18 <li>Mexa sem parar até desgrudar da panela.</li> 19 </ol> 20 </body> 21 </html> 22
  14. 6/10/2019 Introdução ao XPath file:///home/renne/projects/rennerocha/apresentacoes/20190609_CaipyraXPath/index.html?print-pdf 37/50 <li data-qtd="1" data-unit="colher">Manteiga</li> <li

    data-qtd="1" data-unit="lata">Leite Condensado</li> <li data-qtd="1" data-unit="pacote">Choc. Granulado</li> <html> 1 <head> 2 <title>Receita de Brigadeiro</title> 3 </head> 4 <body data-content="receita"> 5 <h1>Ingredientes</h1> 6 <ul> 7 8 9 <li data-qtd="4" data-unit="colher">Chocolate em Pó</li> 10 11 </ul> 12 <h1>Instruções de Preparo</h1> 13 <ol> 14 <li>Aqueça a panela em fogo médio.</li> 15 <li>Acrescente 1 colher de sopa de manteiga.</li> 16 <li>Acrescente o Leite Condensado</li> 17 <li>Acrescente o Chocolate em Pó</li> 18 <li>Mexa sem parar até desgrudar da panela.</li> 19 </ol> 20 </body> 21 </html> 22
  15. 6/10/2019 Introdução ao XPath file:///home/renne/projects/rennerocha/apresentacoes/20190609_CaipyraXPath/index.html?print-pdf 39/50 <li data-qtd="1" data-unit="colher">Manteiga</li> <li

    data-qtd="4" data-unit="colher">Chocolate em Pó</li> <html> 1 <head> 2 <title>Receita de Brigadeiro</title> 3 </head> 4 <body data-content="receita"> 5 <h1>Ingredientes</h1> 6 <ul> 7 8 <li data-qtd="1" data-unit="lata">Leite Condensado</li> 9 10 <li data-qtd="1" data-unit="pacote">Choc. Granulado</li> 11 </ul> 12 <h1>Instruções de Preparo</h1> 13 <ol> 14 <li>Aqueça a panela em fogo médio.</li> 15 <li>Acrescente 1 colher de sopa de manteiga.</li> 16 <li>Acrescente o Leite Condensado</li> 17 <li>Acrescente o Chocolate em Pó</li> 18 <li>Mexa sem parar até desgrudar da panela.</li> 19 </ol> 20 </body> 21 </html> 22
  16. 6/10/2019 Introdução ao XPath file:///home/renne/projects/rennerocha/apresentacoes/20190609_CaipyraXPath/index.html?print-pdf 41/50 <li data-qtd="1" data-unit="colher">Manteiga</li> <html>

    1 <head> 2 <title>Receita de Brigadeiro</title> 3 </head> 4 <body data-content="receita"> 5 <h1>Ingredientes</h1> 6 <ul> 7 8 <li data-qtd="1" data-unit="lata">Leite Condensado</li> 9 <li data-qtd="4" data-unit="colher">Chocolate em Pó</li> 10 <li data-qtd="1" data-unit="pacote">Choc. Granulado</li> 11 </ul> 12 <h1>Instruções de Preparo</h1> 13 <ol> 14 <li>Aqueça a panela em fogo médio.</li> 15 <li>Acrescente 1 colher de sopa de manteiga.</li> 16 <li>Acrescente o Leite Condensado</li> 17 <li>Acrescente o Chocolate em Pó</li> 18 <li>Mexa sem parar até desgrudar da panela.</li> 19 </ol> 20 </body> 21 </html> 22
  17. 6/10/2019 Introdução ao XPath file:///home/renne/projects/rennerocha/apresentacoes/20190609_CaipyraXPath/index.html?print-pdf 43/50 <li data-qtd="4" data-unit="colher">Chocolate em

    Pó</li> <html> 1 <head> 2 <title>Receita de Brigadeiro</title> 3 </head> 4 <body data-content="receita"> 5 <h1>Ingredientes</h1> 6 <ul> 7 <li data-qtd="1" data-unit="colher">Manteiga</li> 8 <li data-qtd="1" data-unit="lata">Leite Condensado</li> 9 10 <li data-qtd="1" data-unit="pacote">Choc. Granulado</li> 11 </ul> 12 <h1>Instruções de Preparo</h1> 13 <ol> 14 <li>Aqueça a panela em fogo médio.</li> 15 <li>Acrescente 1 colher de sopa de manteiga.</li> 16 <li>Acrescente o Leite Condensado</li> 17 <li>Acrescente o Chocolate em Pó</li> 18 <li>Mexa sem parar até desgrudar da panela.</li> 19 </ol> 20 </body> 21 </html> 22
  18. 6/10/2019 Introdução ao XPath file:///home/renne/projects/rennerocha/apresentacoes/20190609_CaipyraXPath/index.html?print-pdf 46/50 <ul> <li data-qtd="1" data-unit="colher">Manteiga</li>

    <li data-qtd="1" data-unit="lata">Leite Condensado</li> <li data-qtd="4" data-unit="colher">Chocolate em Pó</li> <li data-qtd="1" data-unit="pacote">Choc. Granulado</li> </ul> <html> 1 <head> 2 <title>Receita de Brigadeiro</title> 3 </head> 4 <body data-content="receita"> 5 <h1>Ingredientes</h1> 6 7 8 9 10 11 12 <h1>Instruções de Preparo</h1> 13 <ol> 14 <li>Aqueça a panela em fogo médio.</li> 15 <li>Acrescente 1 colher de sopa de manteiga.</li> 16 <li>Acrescente o Leite Condensado</li> 17 <li>Acrescente o Chocolate em Pó</li> 18 <li>Mexa sem parar até desgrudar da panela.</li> 19 </ol> 20 </body> 21 </html> 22
  19. 6/10/2019 Introdução ao XPath file:///home/renne/projects/rennerocha/apresentacoes/20190609_CaipyraXPath/index.html?print-pdf 48/50 <h1>Ingredientes</h1> <h1>Instruções de Preparo</h1>

    <html> 1 <head> 2 <title>Receita de Brigadeiro</title> 3 </head> 4 <body data-content="receita"> 5 6 <ul> 7 <li data-qtd="1" data-unit="colher">Manteiga</li> 8 <li data-qtd="1" data-unit="lata">Leite Condensado</li> 9 <li data-qtd="4" data-unit="colher">Chocolate em Pó</li> 10 <li data-qtd="1" data-unit="pacote">Choc. Granulado</li> 11 </ul> 12 13 <ol> 14 <li>Aqueça a panela em fogo médio.</li> 15 <li>Acrescente 1 colher de sopa de manteiga.</li> 16 <li>Acrescente o Leite Condensado</li> 17 <li>Acrescente o Chocolate em Pó</li> 18 <li>Mexa sem parar até desgrudar da panela.</li> 19 </ol> 20 </body> 21 </html> 22