품격 있는 스파이더

Python으로 하는 웹 스크레이핑

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으로 하는 웹 스크레이핑

나의 스파이더

  • 필수 import
import scrapy
from scrapy.crawler import CrawlerProcess
  • 집중할 부분: 실제 스파이더
class SpiderClassName(scrapy.Spider):
    name = "spider_name"
    # the code for your spider
    ...
  • 스파이더 실행
# initiate a CrawlerProcess
process = CrawlerProcess()

# tell the process which spider to use
process.crawl(YourSpider)

# start the crawling process
process.start()
Python으로 하는 웹 스크레이핑

웹 짜기

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 )
  • start_requests 함수가 필요합니다
  • HTML을 처리할 파서 함수가 최소 하나 필요합니다
Python으로 하는 웹 스크레이핑

함께 웹을 짭니다

Python으로 하는 웹 스크레이핑

Preparing Video For Download...