Scrapy - очистить весь предмет вместо 1 предмета - PullRequest
0 голосов
/ 05 апреля 2019

Мне нужно очистить все предметы, но только 1 предмет - это слом. Мой код раньше работал нормально, но когда я переношу его в другой проект, такой же код, это происходит, я не знаю, почему

Мне нужно получить все элементы в соответствии с размером страницы в start_url

вот мой рабочий код

class HmSalesitemSpider(scrapy.Spider):
    name = 'HM_salesitem'
    allowed_domains = ['www2.hm.com']
    start_urls = ['https://www2.hm.com/en_us/sale/shopbyproductladies/view- 
all.html?sort=stock&image-size=small&image=stillLife&offset=0&page- 
size=3002']

def parse(self, response):  
    for product_item in response.css('li.product-item'):
        url = "https://www2.hm.com/" + product_item.css('a::attr(href)').extract_first() 
    yield scrapy.Request(url=url, callback=self.parse_subpage)

def parse_subpage(self, response):
    item = {
    'title': response.xpath("normalize-space(.//h1[contains(@class, 'primary') and contains(@class, 'product-item-headline')]/text())").extract_first(),
    'sale-price': response.xpath("normalize-space(.//span[@class='price-value']/text())").extract_first(), 
    'regular-price': response.xpath('//script[contains(text(), "whitePrice")]/text()').re_first("'whitePrice'\s?:\s?'([^']+)'"),
    'photo-url': response.css('div.product-detail-main-image-container img::attr(src)').extract_first(),
    'description': response.css('p.pdp-description-text::text').extract_first()

    }   
    yield item

Пожалуйста, помогите. Спасибо

1 Ответ

0 голосов
/ 05 апреля 2019

Кажется, у вас проблемы с отступами. Переместить запрос на выдачу в for цикл:

def parse(self, response):  
    for product_item in response.css('li.product-item'):
        url = "https://www2.hm.com/" + product_item.css('a::attr(href)').get() 
        yield scrapy.Request(url=url, callback=self.parse_subpage)

Или это немного очищенная версия:

def parse(self, response):  
    for link in response.css('li.product-item a::attr(href)').extract():
        yield response.follow(link, self.parse_subpage)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...