Scraping de verdad

Web scraping en Python

Thomas Laetsch

Data Scientist, NYU

Sitio de DataCamp

Web scraping en Python

¿Qué es el div?

# response loaded with HTML from https://www.datacamp.com/courses/all
course_divs = response.css('div.course-block')
print( len(course_divs) )
>>> 185
Web scraping en Python

Inspeccionando course-block

first_div = course_divs[0]

children = first_div.xpath('./*')
print( len(children) ) >>> 3
Web scraping en Python

El primer hijo

first_div = course_divs[0]

children = first_div.xpath('./*')
first_child = children[0]

print( first_child.extract() ) >>> <a class=... />
Web scraping en Python

El segundo hijo

first_div = course_divs[0]

children = first_div.xpath('./*')
second_child = children[1]

print( second_child.extract() ) >>> <div class=... />
Web scraping en Python

El hijo olvidado

first_div = course_divs[0]

children = first_div.xpath('./*')
third_child = children[2]

print( third_child.extract() ) >>> <span class=... />
Web scraping en Python

En listas

  • En un único selector CSS
    links = response.css('div.course-block > a::attr(href)').extract()
    
  • Paso a paso
# paso 1: bloques de curso
course_divs = response.css('div.course-block')

# paso 2: elementos de enlace hrefs = course_divs.xpath('./a/@href')
# paso 3: extraer enlaces links = hrefs.extract()
Web scraping en Python

A la escuela

for l in links:
    print( l )

>>> /courses/free-introduction-to-r
>>> /courses/data-table-data-manipulation-r-tutorial
>>> /courses/dplyr-data-manipulation-r-tutorial
>>> /courses/ggvis-data-visualization-r-tutorial
>>> /courses/reporting-with-r-markdown
>>> /courses/intermediate-r
...
Web scraping en Python

Enlaces conseguidos

Web scraping en Python

Preparing Video For Download...