From 90c1404ca55b8063827868bae31201c46990ba76 Mon Sep 17 00:00:00 2001 From: Yohann Dedy Date: Mon, 28 Jan 2019 01:07:58 +0100 Subject: [PATCH] Mise en place des processeur input/output (urlVideo, titre, date) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit L'URL de la vidéo embedée a été raccourcie de manière à ne pas intégrer les options de hauteur/largeur. Le titre est nettoyé pour ne pas avoir d'espace vide avant et après le texte. Les retours à la ligne sont aussi supprimés lors du scraping. La date est traduite au format J-MM-AAAA en attendant d'être entièrement compatible avec le format datetime. --- CinemScraper/items.py | 39 ++++++++++++++++++++++++--- CinemScraper/spiders/grabVideoData.py | 19 ++++++------- 2 files changed, 46 insertions(+), 12 deletions(-) diff --git a/CinemScraper/items.py b/CinemScraper/items.py index f0339de..f28b7aa 100644 --- a/CinemScraper/items.py +++ b/CinemScraper/items.py @@ -6,15 +6,48 @@ # https://doc.scrapy.org/en/latest/topics/items.html import scrapy +from scrapy.loader.processors import Join, MapCompose, TakeFirst +def format_date(value): + month = { + 'janvier' : '01', + 'février' : '02', + 'mars' : '03', + 'avril' : '04', + 'mai' : '05', + 'juin' : '06', + 'juillet' : '07', + 'août' : '08', + 'septembre' : '09', + 'octobre' : '10', + 'novembre' : '11', + 'décembre' : '12' + } + date = value.split(' ') + yield date[0]+'-'+month[date[1]]+'-'+date[2] + + +def clean_text(value): + text = value.replace('\\n', '') + yield text.strip() + +def clean_url(value): + yield value.split('?')[0] class video(scrapy.Item): - title = scrapy.Field() + title = scrapy.Field( + input_processor = MapCompose(clean_text), + output_processor = Join() + ) secondary_title = scrapy.Field() description = scrapy.Field() - urlVideo = scrapy.Field() + urlVideo = scrapy.Field( + input_processor = MapCompose(clean_url) + ) urlCF = scrapy.Field() - date_event = scrapy.Field() + date_event = scrapy.Field( + input_processor = MapCompose(clean_text, format_date) + ) tags = scrapy.Field() biographies = scrapy.Field() pass diff --git a/CinemScraper/spiders/grabVideoData.py b/CinemScraper/spiders/grabVideoData.py index 0ddb780..f8afb55 100644 --- a/CinemScraper/spiders/grabVideoData.py +++ b/CinemScraper/spiders/grabVideoData.py @@ -1,7 +1,8 @@ # -*- coding: utf-8 -*- import scrapy from scrapy.loader import ItemLoader -from scrapy.loader.processors import Join, MapCompose +from scrapy.loader.processors import Join, MapCompose, TakeFirst +from w3lib.html import remove_tags from CinemScraper.items import video @@ -11,13 +12,13 @@ class GrabvideodataSpider(scrapy.Spider): start_urls = ['http://www.cinematheque.fr/decouvrir.html'] item_fields = { - 'title' : '//h1/text()', - 'secondary_title' : '//h1/span[@class="sub"]/text()', - 'date_event' : '//p[@class="date"]/text()', - 'urlVideo' : '//iframe/@src', - 'description' : '//div[@class="description"]', - 'biographies' : '//div[@class="biographies"]', - 'tags' : '//span[contains(@class, "tag")]/text()' + 'title' : '//h1/text()', + 'secondary_title' : '//h1/span[@class="sub"]/text()', + 'date_event' : '//p[@class="date"]/text()', + 'urlVideo' : '//iframe/@src', + 'description' : '//div[@class="description"]/p', + 'biographies' : '//div[@class="biographies"]', + 'tags' : '//span[contains(@class, "tag")]/text()' } content_xpath = '//div[@id="content"]' @@ -33,7 +34,7 @@ class GrabvideodataSpider(scrapy.Spider): for page in hxs.xpath(self.content_xpath): loader = ItemLoader(item=video(), selector=page) # mettre des processeurs d'entrée ici -# loader.default_input_processor = MapCompose(unicode.strip) +# loader.default_input_processor = MapCompose(remove_tags) loader.default_output_processor = Join() # iteration des champs de l'item video