Gerçekten Scraping

Python ile Web Scraping

Thomas Laetsch

Data Scientist, NYU

DataCamp Sitesi

Python ile Web Scraping

Div nedir?

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

course-block inceleme

first_div = course_divs[0]

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

İlk alt öğe

first_div = course_divs[0]

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

print( first_child.extract() ) >>> <a class=... />
Python ile Web Scraping

İkinci alt öğe

first_div = course_divs[0]

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

print( second_child.extract() ) >>> <div class=... />
Python ile Web Scraping

Unutulan alt öğe

first_div = course_divs[0]

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

print( third_child.extract() ) >>> <span class=... />
Python ile Web Scraping

Listeyleme

  • Tek bir CSS seçicide
    links = response.css('div.course-block > a::attr(href)').extract()
    
  • Adım adım
# adım 1: course-block'lar
course_divs = response.css('div.course-block')

# adım 2: bağlantı öğeleri hrefs = course_divs.xpath('./a/@href')
# adım 3: linkleri çıkar links = hrefs.extract()
Python ile Web Scraping

Bağlantıları Almak

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
...
Python ile Web Scraping

Bağlantılar elde edildi

Python ile Web Scraping

Preparing Video For Download...