Een chique Spider

Webscraping in Python

Thomas Laetsch

Data Scientist, NYU

Jouw 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()
Webscraping in Python

Jouw spider

  • Vereiste imports
import scrapy
from scrapy.crawler import CrawlerProcess
  • Waar we op focussen: de eigenlijke spider
class SpiderClassName(scrapy.Spider):
    name = "spider_name"
    # the code for your spider
    ...
  • De spider draaien
# initiate a CrawlerProcess
process = CrawlerProcess()

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

# start the crawling process
process.start()
Webscraping in Python

Het web weven

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 )
  • Functie start_requests is vereist
  • Minstens één parserfunctie om de HTML te verwerken
Webscraping in Python

Samen weven we het web

Webscraping in Python

Preparing Video For Download...