การร้องขอบริการ

Web Scraping ด้วย Python

Thomas Laetsch

Data Scientist, NYU

ทบทวน 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 ด้วย Python

ทบทวน 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 ด้วย Python

รู้จัก 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 จะสร้างตัวแปร response ให้อัตโนมัติ
  • อาร์กิวเมนต์ url ระบุเว็บไซต์ที่ต้องการดึงข้อมูล
  • อาร์กิวเมนต์ callback ระบุว่าจะส่ง response ไปประมวลผลที่ใด
Web Scraping ด้วย 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 )
Web Scraping ด้วย Python

สรุปการร้องขอ

Web Scraping ด้วย Python

Preparing Video For Download...