Scrapy выпускает пустые JSON / CSV файлы - PullRequest
0 голосов
/ 30 апреля 2020

Я очень новичок в области скрапа и python и могу действительно помочь. У меня есть этот код для работы в командной строке. Я вижу, как он вытаскивает всю нужную информацию, когда она проходит через разные страницы.

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

Вот код

import scrapy
from urlparse import urljoin


class Aberdeenlocations1Spider(scrapy.Spider):
    name = "aberdeenlocations2"
    start_urls = [
        'http://brighthouse.co.uk/store-finder/all-stores',
    ]

    def parse(self, response):
        products = response.xpath('//ul/li/a/@href').extract()
        for p in products:
            url = urljoin(response.url, p)
            yield scrapy.Request(url, callback=self.parse_product)

    def parse_product(self, response):
        for div in response.css('div'):
          yield {
               title: (response.css('title::text').extract()),
               address: (response.css('[itemprop=streetAddress]::text').extract()),
               locality: (response.css('[itemprop=addressLocality]::text').extract()),
               region: (response.css('[itemprop=addressRegion]::text').extract()),
               postcode: (response.css('[itemprop=postalCode]::text').extract()),
               telephone: (response.css('[itemprop=telephone]::text').extract()),
               script: (response.xpath('//div/script').extract()),
               gmaplink: (response.xpath('//div/div/div/p/a/@href').extract_first())
                  }  

Затем я запускаю эту команду на приведенном выше сценарии

scrapy crawl aberdeenlocations2 -o data.json

Что я делаю не так?

1 Ответ

0 голосов
/ 30 апреля 2020

Просто некоторые python ошибки в вашем доходе, я думаю. Вот так я получаю некоторые данные на выходе:

import scrapy
from urlparse import urljoin


class Aberdeenlocations1Spider(scrapy.Spider):
    name = "aberdeenlocations2"
    start_urls = [
        'http://brighthouse.co.uk/store-finder/all-stores',
    ]

    def parse(self, response):
        products = response.xpath('//ul/li/a/@href').extract()
        for p in products:
            url = urljoin(response.url, p)
            yield scrapy.Request(url, callback=self.parse_product)

    def parse_product(self, response):
        # not sure why this loop is there
        for div in response.css('div'):
          yield {
               'title': response.css('title::text').extract(),
               'address': response.css('[itemprop=streetAddress]::text').extract(),
               'locality': response.css('[itemprop=addressLocality]::text').extract(),
               'region': response.css('[itemprop=addressRegion]::text').extract(),
               'postcode': response.css('[itemprop=postalCode]::text').extract(),
               'telephone': response.css('[itemprop=telephone]::text').extract(),
               'script': response.xpath('//div/script').extract(),
               'gmaplink': response.xpath('//div/div/div/p/a/@href').extract_first()
                  }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...