Елегантний павук

Вебскрапінг у Python

Thomas Laetsch

Data Scientist, NYU

Ваш павук

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()
Вебскрапінг у Python

Ваш павук

  • Необхідні імпорти
import scrapy
from scrapy.crawler import CrawlerProcess
  • Частина, на якій зосередимось: власне павук
class SpiderClassName(scrapy.Spider):
    name = "spider_name"
    # the code for your spider
    ...
  • Запуск павука
# initiate a CrawlerProcess
process = CrawlerProcess()

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

# start the crawling process
process.start()
Вебскрапінг у Python

Плетемо павутину

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 )
  • Потрібна функція start_requests
  • Потрібна щонайменше одна функція парсера для обробки HTML-коду
Вебскрапінг у Python

Сплетімо павутину разом

Вебскрапінг у Python

Preparing Video For Download...