Issue
Using the latest version of Scrapy and python 3.8, I want my crawler to scrape the images of some books from a website. But after running the spider (called promotions), I got this log which shows there are items scraped but neither can I see any new folder created in which the images should be stored nor I can open the output. By the way, my spider worked perfectly before, so I just write down all the codes I've recently added to it. This is the log I got:
2020-10-15 09:31:47 [scrapy.utils.log] INFO: Scrapy 1.6.0 started (bot: digikala)
2020-10-15 09:31:47 [scrapy.utils.log] INFO: Versions: lxml 4.5.2.0, libxml2 2.9.10, cssselect 1.1.0, parsel 1.5.2, w3lib 1.21.0, Twisted 20.3.0, Python 3.8.5 (default, Aug 5 2020, 09:44:06) [MSC v.1916 64 bit (AMD64)], pyOpenSSL 19.1.0 (OpenSSL 1.1.1g 21 Apr 2020), cryptography 2.9.2, Platform Windows-10-10.0.19041-SP0
2020-10-15 09:31:47 [scrapy.crawler] INFO: Overridden settings: {'AUTOTHROTTLE_MAX_DELAY': 120, 'AUTOTHROTTLE_START_DELAY': 60, 'BOT_NAME': 'digikala', 'DOWNLOAD_TIMEOUT': 1200, 'FEED_FORMAT': 'xml', 'FEED_URI': '99-07-24.xml', 'NEWSPIDER_MODULE': 'digikala.spiders', 'SPIDER_MODULES': ['digikala.spiders']}
2020-10-15 09:31:47 [scrapy.extensions.telnet] INFO: Telnet Password: 2303b1f7abb0822e
2020-10-15 09:31:47 [scrapy.middleware] INFO: Enabled extensions:
['scrapy.extensions.corestats.CoreStats',
'scrapy.extensions.telnet.TelnetConsole',
'scrapy.extensions.feedexport.FeedExporter',
'scrapy.extensions.logstats.LogStats']
2020-10-15 09:31:48 [scrapy.middleware] INFO: Enabled downloader middlewares:
['scrapy.downloadermiddlewares.httpauth.HttpAuthMiddleware',
'scrapy.downloadermiddlewares.downloadtimeout.DownloadTimeoutMiddleware',
'scrapy.downloadermiddlewares.defaultheaders.DefaultHeadersMiddleware',
'digikala.middlewares.UserAgentRotatorMiddleware',
'scrapy.downloadermiddlewares.retry.RetryMiddleware',
'scrapy.downloadermiddlewares.redirect.MetaRefreshMiddleware',
'scrapy.downloadermiddlewares.httpcompression.HttpCompressionMiddleware',
'scrapy.downloadermiddlewares.redirect.RedirectMiddleware',
'scrapy.downloadermiddlewares.cookies.CookiesMiddleware',
'scrapy.downloadermiddlewares.httpproxy.HttpProxyMiddleware',
'scrapy.downloadermiddlewares.stats.DownloaderStats']
2020-10-15 09:31:48 [scrapy.middleware] INFO: Enabled spider middlewares:
['scrapy.spidermiddlewares.httperror.HttpErrorMiddleware',
'scrapy.spidermiddlewares.offsite.OffsiteMiddleware',
'scrapy.spidermiddlewares.referer.RefererMiddleware',
'scrapy.spidermiddlewares.urllength.UrlLengthMiddleware',
'scrapy.spidermiddlewares.depth.DepthMiddleware']
2020-10-15 09:31:48 [scrapy.middleware] INFO: Enabled item pipelines:
['digikala.pipelines.DigikalaPipeline']
2020-10-15 09:31:48 [scrapy.core.engine] INFO: Spider opened
2020-10-15 09:31:48 [scrapy.extensions.logstats] INFO: Crawled 0 pages (at 0 pages/min), scraped 0 items (at 0 items/min)
2020-10-15 09:31:48 [scrapy.extensions.telnet] INFO: Telnet console listening on 127.0.0.1:6023
2020-10-15 09:31:49 [scrapy.core.engine] DEBUG: Crawled (200) <GET https://www.digikala.com/search/category-book/?type[0]=4844&pageno=1&last_filter=type&last_value=4844&sortby=4> (referer: None)
2020-10-15 09:31:50 [scrapy.core.engine] DEBUG: Crawled (200) <GET https://www.digikala.com/search/category-book/?type%5B0%5D=4844&last_filter=type&last_value=4844&sortby=4&pageno=2> (referer: https://www.digikala.com/search/category-book/?type[0]=4844&pageno=1&last_filter=type&last_value=4844&sortby=4)
This log goes on page by page like above then ends like this:
2020-10-15 09:35:49 [scrapy.core.engine] INFO: Closing spider (finished)
2020-10-15 09:35:49 [scrapy.statscollectors] INFO: Dumping Scrapy stats:
{'downloader/request_bytes': 180826,
'downloader/request_count': 277,
'downloader/request_method_count/GET': 277,
'downloader/response_bytes': 24372358,
'downloader/response_count': 277,
'downloader/response_status_count/200': 277,
'finish_reason': 'finished',
'finish_time': datetime.datetime(2020, 10, 15, 6, 5, 49, 412323),
'log_count/DEBUG': 277,
'log_count/INFO': 13,
'request_depth_max': 276,
'response_received_count': 277,
'scheduler/dequeued': 277,
'scheduler/dequeued/memory': 277,
'scheduler/enqueued': 277,
'scheduler/enqueued/memory': 277,
'start_time': datetime.datetime(2020, 10, 15, 6, 1, 48, 237054)}
2020-10-15 09:35:49 [scrapy.core.engine] INFO: Spider closed (finished)
I can't understand what is going on in this log, so I explain what I did step by step. This is my items.py file:
import scrapy
from scrapy.loader.processors import TakeFirst
class DigikalaItem(scrapy.Item):
image_urls=scrapy.Field()
images=scrapy.Field()
book_name=scrapy.Field(
output_processor=TakeFirst()
)
This pipelines.py file:
from scrapy.pipelines.images import ImagesPipeline
from scrapy import Request
class DigikalaPipeline(ImagesPipeline):
def get_media_requests(self, item, info):
return [Request(x, meta={'bookname':item.get('book_name')}) for x in item.get(self.images_urls_field, [])]
def file_path(self, request, response=None, info=None):
filename= request.meta['bookname'].replace(':', '')
return 'full/%s.jpg' % (filename)
Here is the settings.py file:
ITEM_PIPELINES = {
'digikala.pipelines.DigikalaPipeline': 1,
}
IMAGES_STORE='C:/Users/shima/projects/digikala'
DOWNLOAD_TIMEOUT=1200
And here are the codes I added to my spider (promotions.py file):
from scrapy.loader import ItemLoader
from digikala.items import DigikalaItem
class PromotionsSpider(scrapy.Spider):
name= 'promotions'
allowed_domains=['www.digikala.com']
def start_requests(self):
yield scrapy.Request(url= 'https://www.digikala.com/search/category-book/?type[0]=4844&pageno=1&last_filter=type&last_value=4844&sortby=4',
callback= self.parse)
def parse(self, response):
star=0
for product in response.xpath("//ul[@class='c-listing__items']/li"):
title= product.xpath(".//a[@class='js-product-url']/text()").get()
if product.xpath(".//div[@class='c-product-box__engagement-rating']/text()"):
star= float(str(product.xpath(".//div[@class='c-product-box__engagement-rating']/text()").get()))
else:
star=0
loader=ItemLoader(item=DigikalaItem(), selector=product)
absolute_url=product.xpath(".//a[@class='c-product-box__img c-promotion-box__image js-url js-product-item js-product-url']/img/@src").extract_first()
loader.add_value('image_urls', absolute_url)
image=loader.load_item()
if star>=3.5 and (discounted_amount>=5000 or discounted_percent>=10):
yield{
'image':image,
'title':title,
'star':star
}
Can anyone tell me where the problem is? Thanks a lot!!
Solution
primary problem is you are unable to select products from start page
for product in response.xpath("//ul[@class='c-listing__items']/li"):
ul
you are trying to select has different class attribute
<ul class="c-listing__items js-plp-products-list">
so what i think is
for product in response.xpath("//ul[contains(@class, 'c-listing__items')]/li"):
should work for you
Answered By - akhter wahab
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.