Марат
Марат

Reputation: 3

How do I change the selector scrapy?

How do I change the selector scrapy? During the work of the spider.

Please take a look at my code:

# -*- coding: utf-8 -*-
import scrapy
from kino.items import KinoItem


class KinopoiskSpider(scrapy.Spider):
    name = 'kinopoisk'
    allowed_domains = ['kinopoisk.ru']
    start_urls = ['https://www.kinopoisk.ru/afisha/new/city/1/']

    def parse(self, response):
        links = response.css('div.name>a').xpath('@href').extract()
        for link in links:
            yield scrapy.Request(response.urljoin(link), callback=self.parse_moov, dont_filter=True)

    def parse_moov(self, response):
        item = KinoItem()
        item['orgname'] = response.css('div#headerFilm>span::text').extract()
        item['name'] = response.css('h1.moviename-big::text').extract()
        item['rating'] = response.css('div.block_2>div.div1>a>span.rating_ball::text').extract()
        item['r_critic'] = response.css('div.ratingNum>span::text').extract()
        item['waiting'] = response.xpath('//*[@id="block_rating"]/div[1]/div[3]/a[1]/text()').extract()
        if item['waiting'] is None:
            item['waiting_two'] = response.xpath('//*[@id="block_rating"]/div[1]/div[2]/a[1]/text()').extract()
        item['runtime'] = response.css('td#runtime::text').extract()
        item['premiere'] = response.xpath('//*[@id="div_rus_prem_td2"]/div/span[1]/a[1]/text()').extract()
        item['info'] = response.css('div.brand_words.film-synopsys::text').extract()

        yield item

Here if item['waiting'] is None: item['waiting_two'] does not work. Can someone please suggest some help here.

Upvotes: 0

Views: 292

Answers (1)

Vic
Vic

Reputation: 133

Actually I don't think it's a selector problem.

When the extract() method extract nothing, it will return an empty list. An empty list is not None in Python.

for example:

(same result both in python 2 and python 3)
>>> a = []
>>> a is None
False
>>> not a
True

Which means you should change if item['waiting'] is None to if not item['waiting']

I print the result of the item:

(orgin)
==========================
orgname: []
name: ['Килиманджара']
rating: []
r_critic: []
waiting: [] <==== This is the empty list that extract() returned.
runtime: ['75 мин. ', ' 01:15']
premiere: ['19 июля 2018']
info: ['Когда к\xa0тебе на\xa0свадьбу не\xa0приезжает жених, остается только одно\xa0\x97\xa0найти его. Именно так\xa0и поступает красавица Маруся, отправившись на\xa0поиски своего возлюбленного в\xa0его родной Азербайджан. И\xa0хорошо, что\xa0рядом есть дружная команда друзей, или\xa0не очень дружная и\xa0не совсем друзей. На\xa0такие приключения в\xa0Баку не\xa0рассчитывал никто из\xa0компании Маруси, как\xa0и сами жители.']
==========================

(changed, I print waiting_two before the others)
waiting_two: ['78%']
==========================
orgname: []
name: ['Килиманджара']
rating: []
r_critic: []
waiting: []
runtime: ['75 мин. ', ' 01:15']
premiere: ['19 июля 2018']
info: ['Когда к\xa0тебе на\xa0свадьбу не\xa0приезжает жених, остается только одно\xa0\x97\xa0найти его. Именно так\xa0и поступает красавица Маруся, отправившись на\xa0поиски своего возлюбленного в\xa0его родной Азербайджан. И\xa0хорошо, что\xa0рядом есть дружная команда друзей, или\xa0не очень дружная и\xa0не совсем друзей. На\xa0такие приключения в\xa0Баку не\xa0рассчитывал никто из\xa0компании Маруси, как\xa0и сами жители.']
==========================

Upvotes: 1

Related Questions