Richiesta di servizio

Web Scraping in Python

Thomas Laetsch

Data Scientist, NYU

Ripasso sullo 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 in Python

Ripasso sullo 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 in Python

Il succo di 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 )
  • Qui scrapy.Request valorizza per noi una variabile response
  • L’argomento url indica quale sito fare scraping
  • L’argomento callback indica dove inviare response per l’elaborazione
Web Scraping in Python

Zoom out

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 in Python

Fine richiesta

Web Scraping in Python

Preparing Video For Download...