DataCamp Açıklamaları
Önceki egzersizde olduğu gibi, burada da tüm bir web tarayıcısı spider ile çalıştığın için kod uzun. Ama yine de, kodun miktarı gözünü korkutmasın; artık spider'ların nasıl çalıştığını kavradın ve burada senden istenen basit görevi rahatlıkla tamamlayabilirsin!
Önceki egzersizde olduğu gibi, spider çıktısını önizlemeni sağlayan previewCourses adlı bir fonksiyon oluşturduk; ama kodu çalıştırdıktan sonra dc_dict sözlüğünü de dilediğin gibi keşfedebilirsin.
Bu egzersizde, kurs açıklamasının metnine doğrudan giden bir CSS Locator dizesi oluşturman isteniyor. Bilmen gereken tek şey şu: kurs sayfasında kurs açıklaması metni, course__description (iki alt çizgi) sınıfına ait bir paragraf p öğesinin içinde yer alıyor.
Bu egzersiz
Python ile Web Scraping
kursunun bir parçasıdırEgzersiz talimatları
- Aşağıda
parse_pagesmetodundaki tek boşluğu,course__descriptionsınıfına ait paragrafpöğesinin içindeki metne yönelen bir CSS Locator dizesiyle doldur.
NOT: Eğer Kodu Çalıştır'a basarsan, tekrar başarılı şekilde Kodu Çalıştır kullanabilmek için Örnek koda sıfırla yapman gerekir!!
Uygulamalı interaktif egzersiz
Bu örnek kodu tamamlayarak bu egzersizi bitirin.
# Import scrapy
import scrapy
# Import the CrawlerProcess: for running the spider
from scrapy.crawler import CrawlerProcess
# Create the Spider class
class DC_Description_Spider(scrapy.Spider):
name = "dc_chapter_spider"
# start_requests method
def start_requests(self):
yield scrapy.Request(url = url_short,
callback = self.parse_front)
# First parsing method
def parse_front(self, response):
course_blocks = response.css('div.course-block')
course_links = course_blocks.xpath('./a/@href')
links_to_follow = course_links.extract()
for url in links_to_follow:
yield response.follow(url = url,
callback = self.parse_pages)
# Second parsing method
def parse_pages(self, response):
# Create a SelectorList of the course titles text
crs_title = response.xpath('//h1[contains(@class,"title")]/text()')
# Extract the text and strip it clean
crs_title_ext = crs_title.extract_first().strip()
# Create a SelectorList of course descriptions text
crs_descr = response.css( ____ )
# Extract the text and strip it clean
crs_descr_ext = crs_descr.extract_first().strip()
# Fill in the dictionary
dc_dict[crs_title_ext] = crs_descr_ext
# Initialize the dictionary **outside** of the Spider class
dc_dict = dict()
# Run the Spider
process = CrawlerProcess()
process.crawl(DC_Description_Spider)
process.start()
# Print a preview of courses
previewCourses(dc_dict)