Не удалось получить селектор xpath для списка на html-странице - PullRequest
0 голосов
/ 29 апреля 2019

Невозможно получить правильный xpath для списка элементов. Продолжайте получать пустой список Проблема с выделением всего списка.

Ссылка:

https://globaldrive.ru/moskva/motory/2х-тактный-лодочный-мотор-hangkai-m3.5-hp/

вот HTML-код, который я пытаюсь разобрать

<div id="content_features" class="ty-wysiwyg-content content-features">

            <div class="ty-product-feature">
        <span class="ty-product-feature__label">Бренды:</span>


        <div class="ty-product-feature__value">Hangkai</div>
        </div>
                <div class="ty-product-feature">
        <span class="ty-product-feature__label">Вес:</span>


        <div class="ty-product-feature__value">УТОЧНЯЙТЕ У МЕНЕДЖЕРА<span class="ty-product-feature__suffix">кг</span></div>
        </div>

            </div>

Мой код:

for prop in response.xpath('//div[@id="content_features"]'):
    item['properties'].append(
        {
        'name': prop.xpath('normalize-space(./*[@class="ty-product-feature__label"])').extract_first(),
        'value': prop.xpath('normalize-space(./*[@class="ty-product-feature__value"])').extract_first(),
        }
    )

    yield item

Полный анализатор:

import scrapy


class GlobaldriveruSpider(scrapy.Spider):
    name = 'globaldriveru'
    allowed_domains = ['globaldrive.ru']
    start_urls = ['https://globaldrive.ru/moskva/motory/?items_per_page=500']

    def parse(self, response):
        links = response.xpath('//div[@class="ty-grid-list__item-name"]/a/@href').extract()
        for link in links:
            yield scrapy.Request(response.urljoin(link), callback=self.parse_products, dont_filter=True)
            #yield scrapy.Request(link, callback=self.parse_products, dont_filter=True)

    def parse_products(self, response):
        for parse_products in response.xpath('//div[contains(@class, "container-fluid  products_block_page")]'):
            item = dict()
            item['title'] = response.xpath('//h1[@class="ty-product-block-title"]/text()').extract_first()
            item['price'] = response.xpath('//meta[@itemprop="price"]/@content').get()
            item['available'] = response.xpath('normalize-space(//span[@id="in_stock_info_5511"])').extract_first()
            item['image'] = response.xpath('//meta[@property="og:image"]/@content').get()
            item['brand'] = response.xpath('normalize-space(//div[contains(@class,"ty-features-list")])').get()
            item['department'] = response.xpath('normalize-space(//a[@class="ty-breadcrumbs__a"][2]/text())').extract()
            item['properties'] = list()
            for prop in response.xpath('//div[@id="content_features"]'):
                item['properties'].append(
                      {
                          'name': prop.xpath('normalize-space(./*[@class="ty-product-feature__label"])').extract_first(),
                          'value': prop.xpath('normalize-space(./*[@class="ty-product-feature__value"])').extract_first(),
                      }
                )

            yield item

1 Ответ

1 голос
/ 29 апреля 2019

Ваш код почти правильный, только что внесли некоторые исправления в ваши свойства xpath. Также кажется, что ваш основной цикл «продукт» бесполезен, поэтому я удалил его. Проверьте этот код:

def parse_products(self, response):
    item = dict()
    item['title'] = response.xpath('//h1[@class="ty-product-block-title"]/text()').get()
    item['price'] = response.xpath('//meta[@itemprop="price"]/@content').get()
    item['available'] = response.xpath('normalize-space(//span[@id="in_stock_info_5511"])').get()
    item['image'] = response.xpath('//meta[@property="og:image"]/@content').get()
    item['brand'] = response.xpath('normalize-space(//div[contains(@class,"ty-features-list")])').get()
    item['department'] = response.xpath('normalize-space(//a[@class="ty-breadcrumbs__a"][2]/text())').extract()
    item['properties'] = list()
    for prop in response.xpath('//div[@id="content_features"]/div[@class="ty-product-feature"]'):
        item['properties'].append(
              {
                  'name': prop.xpath('normalize-space(./*[@class="ty-product-feature__label"])').get(),
                  'value': prop.xpath('normalize-space(./*[@class="ty-product-feature__value"])').get(),
              }
        )
    yield item
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...