Laba-laba Berkelas

Web Scraping dengan Python

Thomas Laetsch

Data Scientist, NYU

Spider Anda

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

Spider Anda

  • Impor wajib
import scrapy
from scrapy.crawler import CrawlerProcess
  • Bagian fokus: spider utama
class SpiderClassName(scrapy.Spider):
    name = "spider_name"
    # the code for your spider
    ...
  • Menjalankan spider
# initiate a CrawlerProcess
process = CrawlerProcess()

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

# start the crawling process
process.start()
Web Scraping dengan Python

Merajut 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 )
  • Wajib memiliki fungsi start_requests
  • Wajib memiliki minimal satu fungsi parser untuk menangani kode HTML
Web Scraping dengan Python

Kita Akan Merajut Web Bersama

Web Scraping dengan Python

Preparing Video For Download...