解析を動かそう

Pythonで学ぶWebスクレイピング

Thomas Laetsch

Data Scientist, NYU

もう一度

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 )
Pythonで学ぶWebスクレイピング

すでに知っています!

def parse( self, response ):

# すでに知っている response の入力解析!
# ファイルに出力、または…
# ウェブをクロール!
Pythonで学ぶWebスクレイピング

DataCampコースのリンク:ファイル保存

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] )
Pythonで学ぶWebスクレイピング

DataCampコースのリンク:再解析

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 ): # ここでコースサイトを解析!
Pythonで学ぶWebスクレイピング

DataCampサイト内のリンクをたどるクモ。

Pythonで学ぶWebスクレイピング

ジョニー・パースィン

Pythonで学ぶWebスクレイピング

Preparing Video For Download...