Třída pavouka

Web Scraping v Pythonu

Thomas Laetsch

Data Scientist, NYU

Váš pavouk

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 v Pythonu

Váš pavouk

  • Požadované importy
import scrapy
from scrapy.crawler import CrawlerProcess
  • Část, na kterou se zaměříme: samotný pavouk
class SpiderClassName(scrapy.Spider):
    name = "spider_name"
    # the code for your spider
    ...
  • Spuštění pavouka
# initiate a CrawlerProcess
process = CrawlerProcess()

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

# start the crawling process
process.start()
Web Scraping v Pythonu

Tkaní sítě

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 )
  • Je nutná funkce start_requests
  • Je nutná alespoň jedna parsovací funkce pro zpracování HTML
Web Scraping v Pythonu

Budeme tkát síť společně

Web Scraping v Pythonu

Preparing Video For Download...