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()
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 )
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 인수는 응답 변수를 보낼 처리 함수를 지정합니다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으로 하는 웹 스크레이핑