Navigation XPath

Web Scraping en Python

Thomas Laetsch

Data Scientist, NYU

Slashes et crochets

  • Un seul slash / avance d’une génération
  • Double slash // parcourt toutes les générations suivantes
  • Les crochets [] affinent la sélection d’éléments précis
Web Scraping en Python

Avec ou sans crochets ?

Sélection de body

xpath = '/html/body'
xpath = '/html[1]/body[1]'
  • Donnent la même sélection
Web Scraping en Python

Un body de p

xpath = '/html/body/p'

Sélection de body/p

Web Scraping en Python

Les p du div

xpath = '/html/body/div/p'

Sélection de div/p

xpath = '/html/body/div/p[2]'

Sélection de div/p[2]

Web Scraping en Python

Doubles slash et crochets

xpath = '//p'

Sélection de p avec XPath

xpath = '//p[1]'

Sélection du 1er p dans body

Web Scraping en Python

Le caractère générique

xpath = '/html/body/*'

Sélection de body/*

  • L’astérisque * est le « joker »
Web Scraping en Python

Xposé

Web Scraping en Python

Preparing Video For Download...