Mueve tu maldito parseo

Web scraping en Python

Thomas Laetsch

Data Scientist, NYU

Una vez más

class DCspider( scrapy.Spider ):
    name = "dcspider"

    def start_requests( self ):
        urls = [ 'https://www.datacamp.com/courses/all' ]
        for url in urls:
            yield scrapy.Request( url = url, callback = self.parse )

    def parse( self, response ):
        # simple example: write out the html
        html_file = 'DC_courses.html'
        with open( html_file, 'wb' ) as fout:
            fout.write( response.body )
Web scraping en Python

¡Ya lo sabes!

def parse( self, response ):

# ¡código de parseo de entrada con response que ya conoces!
# salida a un archivo, o...
# ¡rastrea la web!
Web scraping en Python

Enlaces de cursos de DataCamp: guardar en archivo

class DCspider( scrapy.Spider ):
    name = "dcspider"

    def start_requests( self ):
        urls = [ 'https://www.datacamp.com/courses/all' ]
        for url in urls:
            yield scrapy.Request( url = url, callback = self.parse )

def parse( self, response ):
links = response.css('div.course-block > a::attr(href)').extract()
filepath = 'DC_links.csv' with open( filepath, 'w' ) as f: f.writelines( [link + '/n' for link in links] )
Web scraping en Python

Enlaces de cursos de DataCamp: parsear de nuevo

class DCspider( scrapy.Spider ):
    name = "dcspider"

    def start_requests( self ):
        urls = [ 'https://www.datacamp.com/courses/all' ]
        for url in urls:
            yield scrapy.Request( url = url, callback = self.parse )

def parse( self, response ):
links = response.css('div.course-block > a::attr(href)').extract()
for link in links: yield response.follow( url = link, callback = self.parse2 )
def parse2( self, response ): # ¡parsea las páginas de curso aquí!
Web scraping en Python

Una araña siguiendo enlaces por el sitio de DataCamp.

Web scraping en Python

Johnny parseando

Web scraping en Python

Preparing Video For Download...