Commit 9fd0391f authored by Renán Sosa Guillen's avatar Renán Sosa Guillen

crawlers

parent 30b928b1
...@@ -5,13 +5,15 @@ MEDIA: ...@@ -5,13 +5,15 @@ MEDIA:
Diario de Chiapas, Chiapas Diario de Chiapas, Chiapas
USAGE USAGE
$ cd diarioDeChiapas $ cd diarioDeChiapas/
------------------------------------------------------------------------------------------------------------ ------------------------------------------------------------------------------------------------------------
## Get all the news from the most current to the oldest. It's necessary to use the parse_date_files.py file ## Get all the news from the most current to the oldest. It's necessary to use the parse_date_files.py file
for the news contained in noticias.json being splitted into files by date. ## for the news contained in noticias.json being splitted into files by date. ##
$ scrapy crawl noticias --nolog -s filename=noticias.json $ scrapy crawl noticias --nolog -s filename=noticias.json
------------------------------------------------------------------------------------------------------------ ------------------------------------------------------------------------------------------------------------
## Get all the news from the most current to a specific date. ## ## Get all the news from the most current to a specific date. ##
$ scrapy crawl noticias --nolog -s filename=2018-08-30.json -a year=2018 -a month=8 -a day=30 $ scrapy crawl noticias --nolog -s filename=2018-08-30.json -a year=2018 -a month=8 -a day=30
""" """
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
# Define here the models for your scraped items # Define here the models for your scraped items
# #
# See documentation in: # See documentation in:
# http://doc.scrapy.org/en/latest/topics/items.html # https://doc.scrapy.org/en/latest/topics/items.html
import scrapy import scrapy
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
# Define here the models for your spider middleware # Define here the models for your spider middleware
# #
# See documentation in: # See documentation in:
# http://doc.scrapy.org/en/latest/topics/spider-middleware.html # https://doc.scrapy.org/en/latest/topics/spider-middleware.html
from scrapy import signals from scrapy import signals
...@@ -20,14 +20,14 @@ class PrensahnSpiderMiddleware(object): ...@@ -20,14 +20,14 @@ class PrensahnSpiderMiddleware(object):
crawler.signals.connect(s.spider_opened, signal=signals.spider_opened) crawler.signals.connect(s.spider_opened, signal=signals.spider_opened)
return s return s
def process_spider_input(response, spider): def process_spider_input(self, response, spider):
# Called for each response that goes through the spider # Called for each response that goes through the spider
# middleware and into the spider. # middleware and into the spider.
# Should return None or raise an exception. # Should return None or raise an exception.
return None return None
def process_spider_output(response, result, spider): def process_spider_output(self, response, result, spider):
# Called with the results returned from the Spider, after # Called with the results returned from the Spider, after
# it has processed the response. # it has processed the response.
...@@ -35,7 +35,7 @@ class PrensahnSpiderMiddleware(object): ...@@ -35,7 +35,7 @@ class PrensahnSpiderMiddleware(object):
for i in result: for i in result:
yield i yield i
def process_spider_exception(response, exception, spider): def process_spider_exception(self, response, exception, spider):
# Called when a spider or process_spider_input() method # Called when a spider or process_spider_input() method
# (from other spider middleware) raises an exception. # (from other spider middleware) raises an exception.
...@@ -43,7 +43,7 @@ class PrensahnSpiderMiddleware(object): ...@@ -43,7 +43,7 @@ class PrensahnSpiderMiddleware(object):
# or Item objects. # or Item objects.
pass pass
def process_start_requests(start_requests, spider): def process_start_requests(self, start_requests, spider):
# Called with the start requests of the spider, and works # Called with the start requests of the spider, and works
# similarly to the process_spider_output() method, except # similarly to the process_spider_output() method, except
# that it doesn’t have a response associated. # that it doesn’t have a response associated.
...@@ -54,3 +54,50 @@ class PrensahnSpiderMiddleware(object): ...@@ -54,3 +54,50 @@ class PrensahnSpiderMiddleware(object):
def spider_opened(self, spider): def spider_opened(self, spider):
spider.logger.info('Spider opened: %s' % spider.name) spider.logger.info('Spider opened: %s' % spider.name)
class PrensahnDownloaderMiddleware(object):
# Not all methods need to be defined. If a method is not defined,
# scrapy acts as if the downloader middleware does not modify the
# passed objects.
@classmethod
def from_crawler(cls, crawler):
# This method is used by Scrapy to create your spiders.
s = cls()
crawler.signals.connect(s.spider_opened, signal=signals.spider_opened)
return s
def process_request(self, request, spider):
# Called for each request that goes through the downloader
# middleware.
# Must either:
# - return None: continue processing this request
# - or return a Response object
# - or return a Request object
# - or raise IgnoreRequest: process_exception() methods of
# installed downloader middleware will be called
return None
def process_response(self, request, response, spider):
# Called with the response returned from the downloader.
# Must either;
# - return a Response object
# - return a Request object
# - or raise IgnoreRequest
return response
def process_exception(self, request, exception, spider):
# Called when a download handler or a process_request()
# (from other downloader middleware) raises an exception.
# Must either:
# - return None: continue processing this exception
# - return a Response object: stops process_exception() chain
# - return a Request object: stops process_exception() chain
pass
def spider_opened(self, spider):
spider.logger.info('Spider opened: %s' % spider.name)
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
# Define your item pipelines here # Define your item pipelines here
# #
# Don't forget to add your pipeline to the ITEM_PIPELINES setting # Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: http://doc.scrapy.org/en/latest/topics/item-pipeline.html # See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html
import json import json
from collections import OrderedDict from collections import OrderedDict
......
...@@ -5,9 +5,9 @@ ...@@ -5,9 +5,9 @@
# For simplicity, this file contains only settings considered important or # For simplicity, this file contains only settings considered important or
# commonly used. You can find more settings consulting the documentation: # commonly used. You can find more settings consulting the documentation:
# #
# http://doc.scrapy.org/en/latest/topics/settings.html # https://doc.scrapy.org/en/latest/topics/settings.html
# http://scrapy.readthedocs.org/en/latest/topics/downloader-middleware.html # https://doc.scrapy.org/en/latest/topics/downloader-middleware.html
# http://scrapy.readthedocs.org/en/latest/topics/spider-middleware.html # https://doc.scrapy.org/en/latest/topics/spider-middleware.html
BOT_NAME = 'prensaHn' BOT_NAME = 'prensaHn'
...@@ -25,7 +25,7 @@ NEWSPIDER_MODULE = 'prensaHn.spiders' ...@@ -25,7 +25,7 @@ NEWSPIDER_MODULE = 'prensaHn.spiders'
#CONCURRENT_REQUESTS = 32 #CONCURRENT_REQUESTS = 32
# Configure a delay for requests for the same website (default: 0) # Configure a delay for requests for the same website (default: 0)
# See http://scrapy.readthedocs.org/en/latest/topics/settings.html#download-delay # See https://doc.scrapy.org/en/latest/topics/settings.html#download-delay
# See also autothrottle settings and docs # See also autothrottle settings and docs
DOWNLOAD_DELAY = 0.5 DOWNLOAD_DELAY = 0.5
# The download delay setting will honor only one of: # The download delay setting will honor only one of:
...@@ -45,31 +45,31 @@ COOKIES_ENABLED = False ...@@ -45,31 +45,31 @@ COOKIES_ENABLED = False
#} #}
# Enable or disable spider middlewares # Enable or disable spider middlewares
# See http://scrapy.readthedocs.org/en/latest/topics/spider-middleware.html # See https://doc.scrapy.org/en/latest/topics/spider-middleware.html
#SPIDER_MIDDLEWARES = { #SPIDER_MIDDLEWARES = {
# 'prensaHn.middlewares.PrensahnSpiderMiddleware': 543, # 'prensaHn.middlewares.PrensahnSpiderMiddleware': 543,
#} #}
# Enable or disable downloader middlewares # Enable or disable downloader middlewares
# See http://scrapy.readthedocs.org/en/latest/topics/downloader-middleware.html # See https://doc.scrapy.org/en/latest/topics/downloader-middleware.html
#DOWNLOADER_MIDDLEWARES = { #DOWNLOADER_MIDDLEWARES = {
# 'prensaHn.middlewares.MyCustomDownloaderMiddleware': 543, # 'prensaHn.middlewares.PrensahnDownloaderMiddleware': 543,
#} #}
# Enable or disable extensions # Enable or disable extensions
# See http://scrapy.readthedocs.org/en/latest/topics/extensions.html # See https://doc.scrapy.org/en/latest/topics/extensions.html
#EXTENSIONS = { #EXTENSIONS = {
# 'scrapy.extensions.telnet.TelnetConsole': None, # 'scrapy.extensions.telnet.TelnetConsole': None,
#} #}
# Configure item pipelines # Configure item pipelines
# See http://scrapy.readthedocs.org/en/latest/topics/item-pipeline.html # See https://doc.scrapy.org/en/latest/topics/item-pipeline.html
ITEM_PIPELINES = { ITEM_PIPELINES = {
'prensaHn.pipelines.JsonWriterPipeline': 300, 'prensaHn.pipelines.JsonWriterPipeline': 300,
} }
# Enable and configure the AutoThrottle extension (disabled by default) # Enable and configure the AutoThrottle extension (disabled by default)
# See http://doc.scrapy.org/en/latest/topics/autothrottle.html # See https://doc.scrapy.org/en/latest/topics/autothrottle.html
#AUTOTHROTTLE_ENABLED = True #AUTOTHROTTLE_ENABLED = True
# The initial download delay # The initial download delay
#AUTOTHROTTLE_START_DELAY = 5 #AUTOTHROTTLE_START_DELAY = 5
...@@ -82,7 +82,7 @@ ITEM_PIPELINES = { ...@@ -82,7 +82,7 @@ ITEM_PIPELINES = {
#AUTOTHROTTLE_DEBUG = False #AUTOTHROTTLE_DEBUG = False
# Enable and configure HTTP caching (disabled by default) # Enable and configure HTTP caching (disabled by default)
# See http://scrapy.readthedocs.org/en/latest/topics/downloader-middleware.html#httpcache-middleware-settings # See https://doc.scrapy.org/en/latest/topics/downloader-middleware.html#httpcache-middleware-settings
#HTTPCACHE_ENABLED = True #HTTPCACHE_ENABLED = True
#HTTPCACHE_EXPIRATION_SECS = 0 #HTTPCACHE_EXPIRATION_SECS = 0
#HTTPCACHE_DIR = 'httpcache' #HTTPCACHE_DIR = 'httpcache'
......
# Automatically created by: scrapy startproject # Automatically created by: scrapy startproject
# #
# For more information about the [deploy] section see: # For more information about the [deploy] section see:
# https://scrapyd.readthedocs.org/en/latest/deploy.html # https://scrapyd.readthedocs.io/en/latest/deploy.html
[settings] [settings]
default = prensaHn.settings default = prensaHn.settings
......
...@@ -5,13 +5,15 @@ MEDIA: ...@@ -5,13 +5,15 @@ MEDIA:
El Heraldo de Chihuahua, Chihuahua El Heraldo de Chihuahua, Chihuahua
USAGE USAGE
$ cd heraldoChihuahua $ cd heraldoChihuahua/
------------------------------------------------------------------------------------------------------------ ------------------------------------------------------------------------------------------------------------
## Get all the news from the most current to the oldest. It's necessary to use the parse_date_files.py file ## Get all the news from the most current to the oldest. It's necessary to use the parse_date_files.py file
for the news contained in noticias.json being splitted into files by date. ## for the news contained in noticias.json being splitted into files by date. ##
$ scrapy crawl noticias --nolog -s filename=noticias.json $ scrapy crawl noticias --nolog -s filename=noticias.json
------------------------------------------------------------------------------------------------------------ ------------------------------------------------------------------------------------------------------------
## Get all the news from the most current to a specific date. ## ## Get all the news from the most current to a specific date. ##
$ scrapy crawl noticias --nolog -s filename=2018-08-30.json -a year=2018 -a month=8 -a day=30 $ scrapy crawl noticias --nolog -s filename=2018-08-30.json -a year=2018 -a month=8 -a day=30
""" """
......
...@@ -67,21 +67,20 @@ class QuotesSpider(scrapy.Spider): ...@@ -67,21 +67,20 @@ class QuotesSpider(scrapy.Spider):
request.meta['item'] = flow_info request.meta['item'] = flow_info
yield request yield request
else: elif year is None and month is None and day is None:
if year is None and month is None and day is None: self.stop_date = None
self.stop_date = None
for s in section_list:
for s in section_list: flow_info = ImportantFlowData()
flow_info = ImportantFlowData() flow_info['news_section'] = s
flow_info['news_section'] = s
request = scrapy.Request(url=self.base_url + s, callback=self.parse) request = scrapy.Request(url=self.base_url + s, callback=self.parse)
request.meta['item'] = flow_info request.meta['item'] = flow_info
yield request yield request
else: else:
print "Unable to execute this crawler with current given parameters." print "Unable to execute this crawler with current given parameters."
print "Enter all parameters: year, month and day, or none of them." print "Enter all parameters: year, month and day, or none of them."
......
...@@ -5,13 +5,15 @@ MEDIA: ...@@ -5,13 +5,15 @@ MEDIA:
Tinta Fresca, Chiapas Tinta Fresca, Chiapas
USAGE USAGE
$ cd tintaFresca $ cd tintaFresca/
------------------------------------------------------------------------------------------------------------ ------------------------------------------------------------------------------------------------------------
## Get all the news from the most current to the oldest. It's necessary to use the parse_date_files.py file ## Get all the news from the most current to the oldest. It's necessary to use the parse_date_files.py file
for the news contained in noticias.json being splitted into files by date. ## for the news contained in noticias.json being splitted into files by date. ##
$ scrapy crawl noticias --nolog -s filename=noticias.json $ scrapy crawl noticias --nolog -s filename=noticias.json
------------------------------------------------------------------------------------------------------------ ------------------------------------------------------------------------------------------------------------
## Get all the news from the most current to a specific date. ## ## Get all the news from the most current to a specific date. ##
$ scrapy crawl noticias --nolog -s filename=2018-08-30.json -a year=2018 -a month=8 -a day=30 $ scrapy crawl noticias --nolog -s filename=2018-08-30.json -a year=2018 -a month=8 -a day=30
""" """
...@@ -69,8 +71,8 @@ class QuotesSpider(scrapy.Spider): ...@@ -69,8 +71,8 @@ class QuotesSpider(scrapy.Spider):
baseURL = "http://tintafresca.com.mx/" baseURL = "http://tintafresca.com.mx/"
# section_list = ["letras_en_su_tinta/page1/", "tgz/page1/", "patria_chica/page1/", "hecho_en_chiapas/page1/", "show/page1/", "rafaga/page1/"] section_list = ["letras_en_su_tinta/page1/", "tgz/page1/", "patria_chica/page1/", "hecho_en_chiapas/page1/", "show/page1/", "rafaga/page1/"]
section_list = ["tgz/page1/", "patria_chica/page1/", "hecho_en_chiapas/page1/", "show/page1/"] # section_list = ["tgz/page1/", "patria_chica/page1/", "hecho_en_chiapas/page1/", "show/page1/"]
self.month_parser = dict(Enero='01', Febrero='02', Marzo='03', Abril='04', Mayo='05', Junio='06', self.month_parser = dict(Enero='01', Febrero='02', Marzo='03', Abril='04', Mayo='05', Junio='06',
Julio='07', Agosto='08', Septiembre='09', Octubre='10', Noviembre='11', Diciembre='12') Julio='07', Agosto='08', Septiembre='09', Octubre='10', Noviembre='11', Diciembre='12')
......
...@@ -76,7 +76,7 @@ class QuotesSpider(scrapy.Spider): ...@@ -76,7 +76,7 @@ class QuotesSpider(scrapy.Spider):
flow_info = response.meta['item'] flow_info = response.meta['item']
for link in response.css('ul.news-list').xpath('./li/h5/a/@href').extract(): for link in response.css('ul.news-list').xpath('./li/h5/a/@href').extract():
to_next_page = True flow_info['to_next_page'] = True
news_link = self.baseURL + link news_link = self.baseURL + link
yield scrapy.Request(url=news_link, callback=self.parse_item) yield scrapy.Request(url=news_link, callback=self.parse_item)
...@@ -102,7 +102,7 @@ class QuotesSpider(scrapy.Spider): ...@@ -102,7 +102,7 @@ class QuotesSpider(scrapy.Spider):
news_date = datetime(int(self.year), int(self.month), int(self.day), tzinfo=self.tz).isoformat("T") news_date = datetime(int(self.year), int(self.month), int(self.day), tzinfo=self.tz).isoformat("T")
title = response.css('div.post-title').css('h1').extract_first() title = response.css('div.post-title').css('h1').extract_first()
if title is not None : remove_tags(title) if title is not None : title = remove_tags(title)
topic = response.css('div.big-title').xpath('./h2/a/span').extract_first() topic = response.css('div.big-title').xpath('./h2/a/span').extract_first()
if topic is not None : topic = remove_tags(topic) if topic is not None : topic = remove_tags(topic)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment