CSS Locators

Web Scraping in Python

Thomas Laetsch

Data Scientist, NYU

Rosetta CSStone

  • / replace by > (except first character)
    • XPath: /html/body/div
    • CSS Locator: html > body > div
  • // replaced by a blank space (except first character)
    • XPath: //div/span//p
    • CSS Locator: div > span p
  • [N] replaced by :nth-of-type(N)
    • XPath: //div/p[2]
    • CSS Locator: div > p:nth-of-type(2)
Web Scraping in Python

Rosetta CSStone

XPATH

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

CSS

css = 'html > body div > p:nth-of-type(2)'
Web Scraping in Python

Attributes in CSS

  • To find an element by class, use a period .
    • Example: p.class-1 selects all paragraph elements belonging to class-1
  • To find an element by id, use a pound sign #
    • Example: div#uid selects the div element with id equal to uid
Web Scraping in Python

Attributes in CSS

Select paragraph elements within class class1:

css_locator = 'div#uid > p.class1'

Select all elements whose class attribute belongs to class1:

css_locator = '.class1'
Web Scraping in Python

Class Status

css = '.class1'

ClassSelection-Xpath-classonly.png

Web Scraping in Python

Class Status

xpath = '//*[@class="class1"]'

ClassSelection-Xpath-eq.png

Web Scraping in Python

Class Status

xpath = '//*[contains(@class,"class1")]'

ClassSelection-Xpath-contains.png

Web Scraping in Python

Selectors with CSS

from scrapy import Selector 

html = '''
<html>
  <body>
    <div class="hello datacamp">
      <p>Hello World!</p>
    </div>
    <p>Enjoy DataCamp!</p>
  </body>
</html>
''' 
sel = Selector( text = html )
>>> sel.css("div > p")
out: [<Selector xpath='...' data='<p>Hello World!</p>'>] 

>>> sel.css("div > p").extract()
out: [ '<p>Hello World!</p>' ]
Web Scraping in Python

C(SS) You Soon!

Web Scraping in Python

Preparing Video For Download...