Permintaan Layanan

Web Scraping dengan Python

Thomas Laetsch

Data Scientist, NYU

Mengingat Spider

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()
Web Scraping dengan Python

Mengingat Spider

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 )
Web Scraping dengan Python

Inti dari 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 di sini akan mengisi variabel response untuk kita
  • Argumen url menunjukkan situs yang akan di-scrape
  • Argumen callback menentukan ke mana mengirim variabel response untuk diproses
Web Scraping dengan Python

Gambaran Umum

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 )
Web Scraping dengan Python

Akhiri Request

Web Scraping dengan Python

Preparing Video For Download...