from urllib.parse import urljoin

import scrapy


class ComicSpider(scrapy.Spider):
    name = "My spider"

    def __init__(self, *args, **kwargs):
        self.start_urls = kwargs.get('start_urls') or []
        self.next_page_selector = kwargs.get('next_page_selector', None)
        self.comic_image_selector = kwargs.get('comic_image_selector', None)
        super(ComicSpider, self).__init__(*args, **kwargs)

    def parse(self, response):
        comic_image_url = response.xpath(
            self.comic_image_selector).extract_first()

        page = response.meta.get('page') or 1
        yield {
            "image_element": urljoin(response.url, comic_image_url),
            "page": page
        }
        next_page_url = response.xpath(self.next_page_selector).extract_first()
        if next_page_url is not None and not next_page_url.endswith('#'):
            yield scrapy.Request(
                response.urljoin(next_page_url), meta={'page': page + 1})

from urllib.parse import urljoin

import click
import scrapy


class ComicSpider(scrapy.Spider):
    name = "Comic Spider"

    def __init__(self, *args, **kwargs):
        self.start_urls = kwargs.get('start_urls') or []
        self.next_page_selector = kwargs.get('next_page_selector', None)
        self.comic_image_selector = kwargs.get('comic_image_selector', None)
        super(ComicSpider, self).__init__(*args, **kwargs)

    def parse(self, response):
        click.echo("Downloading page {}".format(response.url))
        comic_image_url = response.xpath(
            self.comic_image_selector).extract_first()

        page = response.meta.get('page') or 1
        if comic_image_url is not None:
            yield {
                "image_element": urljoin(response.url, comic_image_url),
                "page": page
            }
        else:
            click.echo("Could not find comic image.")
        next_page_url = response.xpath(self.next_page_selector).extract_first()
        if next_page_url is not None and not next_page_url.endswith('#'):
            yield scrapy.Request(
                response.urljoin(next_page_url), meta={'page': page + 1})

Copy logging from previous version and only yield item to pipeline if a comic image was found
Copy logging from previous version and only yield item to pipeline if a comic image was found

  from urllib.parse import urljoin
  
+ import click
  import scrapy
  
  
  class ComicSpider(scrapy.Spider):
-     name = "My spider"
+     name = "Comic Spider"
  
      def __init__(self, *args, **kwargs):
          self.start_urls = kwargs.get('start_urls') or []
          self.next_page_selector = kwargs.get('next_page_selector', None)
          self.comic_image_selector = kwargs.get('comic_image_selector', None)
          super(ComicSpider, self).__init__(*args, **kwargs)
  
      def parse(self, response):
+         click.echo("Downloading page {}".format(response.url))
          comic_image_url = response.xpath(
              self.comic_image_selector).extract_first()
  
          page = response.meta.get('page') or 1
+         if comic_image_url is not None:
-         yield {
+             yield {
-             "image_element": urljoin(response.url, comic_image_url),
+                 "image_element": urljoin(response.url, comic_image_url),
-             "page": page
+                 "page": page
-         }
+             }
+         else:
+             click.echo("Could not find comic image.")
          next_page_url = response.xpath(self.next_page_selector).extract_first()
          if next_page_url is not None and not next_page_url.endswith('#'):
              yield scrapy.Request(
                  response.urljoin(next_page_url), meta={'page': page + 1})
  
Copy logging from previous version and only yield item to pipeline if a comic image was found
## Code Before:
from urllib.parse import urljoin

import scrapy


class ComicSpider(scrapy.Spider):
    name = "My spider"

    def __init__(self, *args, **kwargs):
        self.start_urls = kwargs.get('start_urls') or []
        self.next_page_selector = kwargs.get('next_page_selector', None)
        self.comic_image_selector = kwargs.get('comic_image_selector', None)
        super(ComicSpider, self).__init__(*args, **kwargs)

    def parse(self, response):
        comic_image_url = response.xpath(
            self.comic_image_selector).extract_first()

        page = response.meta.get('page') or 1
        yield {
            "image_element": urljoin(response.url, comic_image_url),
            "page": page
        }
        next_page_url = response.xpath(self.next_page_selector).extract_first()
        if next_page_url is not None and not next_page_url.endswith('#'):
            yield scrapy.Request(
                response.urljoin(next_page_url), meta={'page': page + 1})

## Instruction:
Copy logging from previous version and only yield item to pipeline if a comic image was found
## Code After:
from urllib.parse import urljoin

import click
import scrapy


class ComicSpider(scrapy.Spider):
    name = "Comic Spider"

    def __init__(self, *args, **kwargs):
        self.start_urls = kwargs.get('start_urls') or []
        self.next_page_selector = kwargs.get('next_page_selector', None)
        self.comic_image_selector = kwargs.get('comic_image_selector', None)
        super(ComicSpider, self).__init__(*args, **kwargs)

    def parse(self, response):
        click.echo("Downloading page {}".format(response.url))
        comic_image_url = response.xpath(
            self.comic_image_selector).extract_first()

        page = response.meta.get('page') or 1
        if comic_image_url is not None:
            yield {
                "image_element": urljoin(response.url, comic_image_url),
                "page": page
            }
        else:
            click.echo("Could not find comic image.")
        next_page_url = response.xpath(self.next_page_selector).extract_first()
        if next_page_url is not None and not next_page_url.endswith('#'):
            yield scrapy.Request(
                response.urljoin(next_page_url), meta={'page': page + 1})
