Eine Serviceanfrage

Web Scraping in Python

Thomas Laetsch

Data Scientist, NYU

Spider-Wiederholung

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

Spider-Wiederholung

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

Kurz zu 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 setzt hier für uns eine response-Variable
  • Das Argument url gibt an, welche Seite gecrawlt wird
  • Das Argument callback gibt an, wohin die response zur Verarbeitung geht
Web Scraping in Python

Überblick

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

Request beenden

Web Scraping in Python

Preparing Video For Download...