Introduction to the scrapy Selector

Web Scraping in Python

Thomas Laetsch

Data Scientist, NYU

Setting up a Selector

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 )
  • Created a scrapy Selector object using a string with the html code

  • The selector sel has selected the entire html document

Web Scraping in Python

Selecting Selectors

  • We can use the xpath call within a Selector to create new Selectors of specific pieces of the html code

  • The return is a SelectorList of Selector objects

sel.xpath("//p")

# outputs the SelectorList: [<Selector xpath='//p' data='<p>Hello World!</p>'>, <Selector xpath='//p' data='<p>Enjoy DataCamp!</p>'>]
Web Scraping in Python

Extracting Data from a SelectorList

  • Use the extract() method
>>> sel.xpath("//p")

out: [<Selector xpath='//p' data='<p>Hello World!</p>'>, <Selector xpath='//p' data='<p>Enjoy DataCamp!</p>'>]
>>> sel.xpath("//p").extract()

out: [ '<p>Hello World!</p>', '<p>Enjoy DataCamp!</p>' ]
  • We can use extract_first() to get the first element of the list
>>> sel.xpath("//p").extract_first()

out: '<p>Hello World!</p>'
Web Scraping in Python

Extracting Data from a Selector

ps = sel.xpath('//p')

second_p = ps[1]
second_p.extract()

out: '<p>Enjoy DataCamp!</p>'
Web Scraping in Python

Select This Course!

Web Scraping in Python

Preparing Video For Download...