サービスのリクエスト

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

Thomas Laetsch

Data Scientist, NYU

スパイダーの復習

import scrapy
from scrapy.crawler import CrawlerProcess

class SpiderClassName(scrapy.Spider):
    name = "spider_name"
    # the code for your spider
    ...

process = CrawlerProcess()

process.crawl(SpiderClassName)

process.start()
Pythonで学ぶWebスクレイピング

スパイダーの復習

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

    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スクレイピング

start_requests の基本

def start_requests( self ):

urls = ['https://www.datacamp.com/courses/all']
for url in urls: yield scrapy.Request( url = url, callback = self.parse )
def start_requests( self ):
    url = 'https://www.datacamp.com/courses/all'
    yield scrapy.Request( url = url, callback = self.parse )
  • ここでの scrapy.Request は、レスポンス変数を用意します
  • url 引数は、スクレイプするサイトを指定します
  • callback 引数は、レスポンス変数の処理先を指定します
Pythonで学ぶWebスクレイピング

全体像

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

    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スクレイピング

リクエストの終了

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

Preparing Video For Download...