From c527a2ff2c33142586cb6e06b7a9b55600cc471e Mon Sep 17 00:00:00 2001 From: dushyant7917 Date: Thu, 12 Oct 2017 21:00:37 +0530 Subject: [PATCH 1/2] crawler to crawl news from livehindustan.com --- LiveHindustan/LiveHindustan/__init__.py | 0 LiveHindustan/LiveHindustan/__init__.pyc | Bin 0 -> 184 bytes LiveHindustan/LiveHindustan/items.py | 14 +++ LiveHindustan/LiveHindustan/middlewares.py | 56 +++++++++++ LiveHindustan/LiveHindustan/pipelines.py | 11 +++ LiveHindustan/LiveHindustan/settings.py | 90 ++++++++++++++++++ LiveHindustan/LiveHindustan/settings.pyc | Bin 0 -> 342 bytes .../LiveHindustan/spiders/__init__.py | 4 + .../LiveHindustan/spiders/__init__.pyc | Bin 0 -> 192 bytes .../LiveHindustan/spiders/news_spider.py | 60 ++++++++++++ .../LiveHindustan/spiders/news_spider.pyc | Bin 0 -> 3289 bytes LiveHindustan/README.md | 16 ++++ LiveHindustan/requirements.txt | 26 +++++ LiveHindustan/scrapy.cfg | 11 +++ 14 files changed, 288 insertions(+) create mode 100644 LiveHindustan/LiveHindustan/__init__.py create mode 100644 LiveHindustan/LiveHindustan/__init__.pyc create mode 100644 LiveHindustan/LiveHindustan/items.py create mode 100644 LiveHindustan/LiveHindustan/middlewares.py create mode 100644 LiveHindustan/LiveHindustan/pipelines.py create mode 100644 LiveHindustan/LiveHindustan/settings.py create mode 100644 LiveHindustan/LiveHindustan/settings.pyc create mode 100644 LiveHindustan/LiveHindustan/spiders/__init__.py create mode 100644 LiveHindustan/LiveHindustan/spiders/__init__.pyc create mode 100644 LiveHindustan/LiveHindustan/spiders/news_spider.py create mode 100644 LiveHindustan/LiveHindustan/spiders/news_spider.pyc create mode 100644 LiveHindustan/README.md create mode 100644 LiveHindustan/requirements.txt create mode 100644 LiveHindustan/scrapy.cfg diff --git a/LiveHindustan/LiveHindustan/__init__.py b/LiveHindustan/LiveHindustan/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/LiveHindustan/LiveHindustan/__init__.pyc b/LiveHindustan/LiveHindustan/__init__.pyc new file mode 100644 index 0000000000000000000000000000000000000000..469f1d45dab0dc5400d3df752ae20c327c6f819d GIT binary patch literal 184 zcmZSn%*%Dg=1ycX0~9ati19BW literal 0 HcmV?d00001 diff --git a/LiveHindustan/LiveHindustan/items.py b/LiveHindustan/LiveHindustan/items.py new file mode 100644 index 0000000..b2abb44 --- /dev/null +++ b/LiveHindustan/LiveHindustan/items.py @@ -0,0 +1,14 @@ +# -*- coding: utf-8 -*- + +# Define here the models for your scraped items +# +# See documentation in: +# http://doc.scrapy.org/en/latest/topics/items.html + +import scrapy + + +class LivehindustanItem(scrapy.Item): + # define the fields for your item here like: + # name = scrapy.Field() + pass diff --git a/LiveHindustan/LiveHindustan/middlewares.py b/LiveHindustan/LiveHindustan/middlewares.py new file mode 100644 index 0000000..99ff5d6 --- /dev/null +++ b/LiveHindustan/LiveHindustan/middlewares.py @@ -0,0 +1,56 @@ +# -*- coding: utf-8 -*- + +# Define here the models for your spider middleware +# +# See documentation in: +# http://doc.scrapy.org/en/latest/topics/spider-middleware.html + +from scrapy import signals + + +class LivehindustanSpiderMiddleware(object): + # Not all methods need to be defined. If a method is not defined, + # scrapy acts as if the spider 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_spider_input(self, response, spider): + # Called for each response that goes through the spider + # middleware and into the spider. + + # Should return None or raise an exception. + return None + + def process_spider_output(self, response, result, spider): + # Called with the results returned from the Spider, after + # it has processed the response. + + # Must return an iterable of Request, dict or Item objects. + for i in result: + yield i + + def process_spider_exception(self, response, exception, spider): + # Called when a spider or process_spider_input() method + # (from other spider middleware) raises an exception. + + # Should return either None or an iterable of Response, dict + # or Item objects. + pass + + def process_start_requests(self, start_requests, spider): + # Called with the start requests of the spider, and works + # similarly to the process_spider_output() method, except + # that it doesn’t have a response associated. + + # Must return only requests (not items). + for r in start_requests: + yield r + + def spider_opened(self, spider): + spider.logger.info('Spider opened: %s' % spider.name) diff --git a/LiveHindustan/LiveHindustan/pipelines.py b/LiveHindustan/LiveHindustan/pipelines.py new file mode 100644 index 0000000..92463e9 --- /dev/null +++ b/LiveHindustan/LiveHindustan/pipelines.py @@ -0,0 +1,11 @@ +# -*- coding: utf-8 -*- + +# Define your item pipelines here +# +# Don't forget to add your pipeline to the ITEM_PIPELINES setting +# See: http://doc.scrapy.org/en/latest/topics/item-pipeline.html + + +class LivehindustanPipeline(object): + def process_item(self, item, spider): + return item diff --git a/LiveHindustan/LiveHindustan/settings.py b/LiveHindustan/LiveHindustan/settings.py new file mode 100644 index 0000000..4cdc37e --- /dev/null +++ b/LiveHindustan/LiveHindustan/settings.py @@ -0,0 +1,90 @@ +# -*- coding: utf-8 -*- + +# Scrapy settings for LiveHindustan project +# +# For simplicity, this file contains only settings considered important or +# commonly used. You can find more settings consulting the documentation: +# +# http://doc.scrapy.org/en/latest/topics/settings.html +# http://scrapy.readthedocs.org/en/latest/topics/downloader-middleware.html +# http://scrapy.readthedocs.org/en/latest/topics/spider-middleware.html + +BOT_NAME = 'LiveHindustan' + +SPIDER_MODULES = ['LiveHindustan.spiders'] +NEWSPIDER_MODULE = 'LiveHindustan.spiders' + + +# Crawl responsibly by identifying yourself (and your website) on the user-agent +#USER_AGENT = 'LiveHindustan (+http://www.yourdomain.com)' + +# Obey robots.txt rules +ROBOTSTXT_OBEY = True + +# Configure maximum concurrent requests performed by Scrapy (default: 16) +#CONCURRENT_REQUESTS = 32 + +# Configure a delay for requests for the same website (default: 0) +# See http://scrapy.readthedocs.org/en/latest/topics/settings.html#download-delay +# See also autothrottle settings and docs +#DOWNLOAD_DELAY = 3 +# The download delay setting will honor only one of: +#CONCURRENT_REQUESTS_PER_DOMAIN = 16 +#CONCURRENT_REQUESTS_PER_IP = 16 + +# Disable cookies (enabled by default) +#COOKIES_ENABLED = False + +# Disable Telnet Console (enabled by default) +#TELNETCONSOLE_ENABLED = False + +# Override the default request headers: +#DEFAULT_REQUEST_HEADERS = { +# 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', +# 'Accept-Language': 'en', +#} + +# Enable or disable spider middlewares +# See http://scrapy.readthedocs.org/en/latest/topics/spider-middleware.html +#SPIDER_MIDDLEWARES = { +# 'LiveHindustan.middlewares.LivehindustanSpiderMiddleware': 543, +#} + +# Enable or disable downloader middlewares +# See http://scrapy.readthedocs.org/en/latest/topics/downloader-middleware.html +#DOWNLOADER_MIDDLEWARES = { +# 'LiveHindustan.middlewares.MyCustomDownloaderMiddleware': 543, +#} + +# Enable or disable extensions +# See http://scrapy.readthedocs.org/en/latest/topics/extensions.html +#EXTENSIONS = { +# 'scrapy.extensions.telnet.TelnetConsole': None, +#} + +# Configure item pipelines +# See http://scrapy.readthedocs.org/en/latest/topics/item-pipeline.html +#ITEM_PIPELINES = { +# 'LiveHindustan.pipelines.LivehindustanPipeline': 300, +#} + +# Enable and configure the AutoThrottle extension (disabled by default) +# See http://doc.scrapy.org/en/latest/topics/autothrottle.html +#AUTOTHROTTLE_ENABLED = True +# The initial download delay +#AUTOTHROTTLE_START_DELAY = 5 +# The maximum download delay to be set in case of high latencies +#AUTOTHROTTLE_MAX_DELAY = 60 +# The average number of requests Scrapy should be sending in parallel to +# each remote server +#AUTOTHROTTLE_TARGET_CONCURRENCY = 1.0 +# Enable showing throttling stats for every response received: +#AUTOTHROTTLE_DEBUG = False + +# Enable and configure HTTP caching (disabled by default) +# See http://scrapy.readthedocs.org/en/latest/topics/downloader-middleware.html#httpcache-middleware-settings +#HTTPCACHE_ENABLED = True +#HTTPCACHE_EXPIRATION_SECS = 0 +#HTTPCACHE_DIR = 'httpcache' +#HTTPCACHE_IGNORE_HTTP_CODES = [] +#HTTPCACHE_STORAGE = 'scrapy.extensions.httpcache.FilesystemCacheStorage' diff --git a/LiveHindustan/LiveHindustan/settings.pyc b/LiveHindustan/LiveHindustan/settings.pyc new file mode 100644 index 0000000000000000000000000000000000000000..5dbb49ddb1fb656fc054cc9766db4faf047b0896 GIT binary patch literal 342 zcmZ9H-%7(U6vj{5S^wa~8?SfUs|Bx$AVX&Z3R^2nMc11Wn$QN;Y&5B|+dZdm;R|>= z1la=lCEs^)$anmo;l*z9y)5Bx5j-Do+Yv$nW0+ymu9J^<+f4}E;&hKQN| zRL|PT&N^Z2&9O7K8!c69lklYPI73X+tVkZ`%w3`2uP-rM6!SEGn=$UL(Ms6+f$Iin zW^JeT4T}`}dG?VN>69%)EWtmlKuPO$qbLTdx5BvS?lz)vMEi`Is|GDyTCPM}*34Ae qDEicjuN7vb2eTY0tDMuOvg6HmKgE5smfcECF7ZbjAOi241gF1)&Q*l~ literal 0 HcmV?d00001 diff --git a/LiveHindustan/LiveHindustan/spiders/__init__.py b/LiveHindustan/LiveHindustan/spiders/__init__.py new file mode 100644 index 0000000..ebd689a --- /dev/null +++ b/LiveHindustan/LiveHindustan/spiders/__init__.py @@ -0,0 +1,4 @@ +# This package will contain the spiders of your Scrapy project +# +# Please refer to the documentation for information on how to create and manage +# your spiders. diff --git a/LiveHindustan/LiveHindustan/spiders/__init__.pyc b/LiveHindustan/LiveHindustan/spiders/__init__.pyc new file mode 100644 index 0000000000000000000000000000000000000000..90725312817187922d5394fec776d7b71f83cbf5 GIT binary patch literal 192 zcmZSn%*%Dg=1ycX0~9aW0!fAldHEm>FZ#VWi_i%1E zO@hu+At03xd_+iyk1j%hDujg8Pw-Rlf8Yne%$)PAguhXr_cc4OotfR4)ZYVrQ(yh{ zz=8HJh2u0f^)Yq={|0yfi^<#s&j8f-i6*1)>WiK&=aw4G`S~+yZ#8*iEFR zd>Yxf|KM*fZ{Sq-Vpo!$baWUw1R;Ucu3%Gn>?!~a12#@T3~+Vn2sBKH4X6RcCTtmC ztPa2e0!qxU&6T9s^|}m1o_Og#>^ADQu9n?kIn-UabhY#~33N#aaHmjlu&SXiEwQGm zn|7ctesJNUb@ie(Ydb4Cd@Q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$#J Date: Thu, 12 Oct 2017 21:03:52 +0530 Subject: [PATCH 2/2] comment added to news_spider.py --- LiveHindustan/LiveHindustan/spiders/news_spider.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/LiveHindustan/LiveHindustan/spiders/news_spider.py b/LiveHindustan/LiveHindustan/spiders/news_spider.py index b537b3d..aa8d40b 100644 --- a/LiveHindustan/LiveHindustan/spiders/news_spider.py +++ b/LiveHindustan/LiveHindustan/spiders/news_spider.py @@ -35,9 +35,6 @@ def parse(self, response): news_summary = scrape_data(Selector(response).xpath('//div[@class="upper-first "]/div/p/text()').extract()) - hindi_month = ['जनवरी,','फरवरी,','मार्च,','अप्रैल,','मई,','जून,','जुलाई,','अगस्त,','सितंबर,','अक्तूबर,','नवम्बर,','दिसम्बर,'] - english_month = ['JAN','FEB','MAR','APR','MAY','JUN','JUL','AUG','SEP','OCT','NOV','DEC'] - news_date_time = scrape_data(Selector(response).xpath('//div[@class="list-time-tags tags-list"]/span/text()[not(ancestor::*[@class="list-tags"])]').extract()) date_time_list = [] for i in news_date_time: @@ -47,6 +44,8 @@ def parse(self, response): date_time_list.append(i) + # The below code just prints the news + # It can be processed to do something like inserting it in a database etc. for i,j,k,l,m in zip(news_titles,news_urls,image_urls,news_summary,date_time_list): print "News title : " + i print "News link : " + 'http://www.livehindustan.com' + j