Latihan 1 : Crawl Seluruh Isi quotes.toscrape.com

Pada modul ini, kita akan berlatih membuat spider untuk crawaling seluruh quote dari http://quotes.toscrape.com/

Silakan buat project baru, kemudian buat spider baru.

scrapy startproject nama_project
scrapy genspider nama_spider alamat_domain

#contoh:
scrapy startproject quotesall_crawl
scrapy genspider quotesall quotes.toscrape.com

Buka file quotesall.py, lalu ketikan code dibawah:

import scrapy


class QuotesallSpider(scrapy.Spider):
    name = 'quotesall'
    allowed_domains = ['quotes.toscrape.com']
    start_urls = ['http://quotes.toscrape.com/']

    def parse(self, response):
        quotes = response.xpath('//*[@class="quote"]')
        for quote in quotes:
            text = quote.xpath('.//*[@class="text"]/text()').extract_first()
            author = quote.xpath('.//*[@itemprop="author"]/text()').extract_first()
            tags = quote.xpath('.//*[@itemprop="keywords"]/@content').extract_first()

            yield{'text': text,
                  'author': author,
                  'tags': tags}

        next_url = quote.xpath('//*[@class="next"]/a/@href').extract_first()
        the_next_url = response.urljoin(next_url)
        yield scrapy.Request(the_next_url)

Jalankan spider dengan perintah scrapy crawl quotesall, log akan menunjukan hasil yield. Sebagai perbandingan, bisa dilihat bagian INFO: Dumping Scrapy stats, yang pelu diperhatikan adalah downloader/request_method_count/GET’: 10, berarti spider berhasil crawling 10 halaman dari web quotes.toscrape.com.

....
....
2021-02-04 16:43:08 [scrapy.statscollectors] INFO: Dumping Scrapy stats:
{'downloader/request_bytes': 2692,
 'downloader/request_count': 10,
 'downloader/request_method_count/GET': 10,
 'downloader/response_bytes': 23163,
 'downloader/response_count': 10,
 'downloader/response_status_count/200': 10,
 'dupefilter/filtered': 1,
 'elapsed_time_seconds': 6.059947,
 'finish_reason': 'finished',
 'finish_time': datetime.datetime(2021, 2, 4, 9, 43, 8, 69379),
 'item_scraped_count': 100,
 'log_count/DEBUG': 111,
 'log_count/INFO': 10,
 'request_depth_max': 10,
 'response_received_count': 10,
 'scheduler/dequeued': 10,
 'scheduler/dequeued/memory': 10,
 'scheduler/enqueued': 10,
 'scheduler/enqueued/memory': 10,
 'start_time': datetime.datetime(2021, 2, 4, 9, 43, 2, 9432)}
2021-02-04 16:43:08 [scrapy.core.engine] INFO: Spider closed (finished)

Kelebihan menggunakan perintah yield pada program diatas, kita bisa langsung menyimpan ke file dengan menggunakan perintah scrapy -o.

Berikut code untuk menyimpan file dalam format csv.

scrapy crawl quotesall -o allquotes.csv

Pada log juga akan ditampilkan info penyimpanan file tersebut. File dapat diakses di root directory dari project.

....
....
2021-02-04 16:56:56 [scrapy.core.engine] INFO: Closing spider (finished)
2021-02-04 16:56:56 [scrapy.extensions.feedexport] INFO: Stored csv feed (100 items) in: allquotes.csv
....
....

Untuk format lainya yang disupport adalah json dan XML, dengan perintah yang sama cukup mengganti file extension saja.

scrapy crawl quotesall -o allquotes.xml
scrapy crawl quotesall -o allquotes.json

Sharing is caring:

Leave a Comment