Как извлечь все изображения, которые находятся на другой странице (дочерней странице), используя xpath и scrapy - PullRequest
0 голосов
/ 04 июля 2019

Я пытаюсь извлечь список всех URL-адресов изображений из https://www.rawson.co.za/property/for-sale/cape-town. Однако все изображения доступны не на основной, а на другой странице.Я использовал xpath для получения других нужных полей.

Я не совсем уверен, как получить все URL-адреса в списке с этих дочерних страниц.Это то, что я пробовал:


    class PropDataSpider(scrapy.Spider):
        name = "rawson"
        start_urls = ['https://www.rawson.co.za/property/for-sale/cape-town']


        def parse(self, response):
            propertes = response.xpath("//div[@class='card__main']")
            for prop in propertes:
                title = prop.xpath(
                    "./div[@class='card__body']/h3[@class='card__title']/a/text()").extract_first()
                price = prop.xpath(
                    "./div[@class='card__body']/div[@class='card__footer card__footer--primary']/div[@class='card__price']/text()").extract_first()
                description = prop.xpath(
                    "./div[@class='card__body']/div[@class='card__synopsis']/p/text()").extract_first()
                bedrooms = prop.xpath(
                    "./div[@class='card__body']/div[@class='card__footer card__footer--primary']/div[@class='features features--inline']/ol[@class ='features__list']/li[@class ='features__item'][1]/div[@class='features__label']/text()").extract_first()

    ...



                images = ['https://' + img for img in prop.xpath(
                    "main[@class='l-main']/section[@class='l-section']/div[@class='l-wrapper']/div[@class='l-section__main']/div[@class ='content-block content-block--flat']/div[@class ='gallery gallery--flat js-lightbox']/div[@ class ='row row--flat']/div[@class ='col']/a[@class ='gallery__link js-lightbox-image']/img/@src")]

                yield {'title': title, 'price':price, "description": description, 'bedrooms': bedrooms, 'bathrooms': bathrooms, 'garages': garages, 'images':images}

Но этот код действительно получает «Нет» для изображений, что имеет смысл, однако я не уверен, как это сделать.Если у кого-то есть предложение, оно будет очень признательно.Заранее спасибо!

1 Ответ

0 голосов
/ 04 июля 2019

Вам нужно использовать response.meta:

def parse(self, response):
    propertes = response.xpath("//div[@class='card__main']")
    for prop in propertes:
        property_url = prop.xpath(
            "./div[@class='card__body']/h3[@class='card__title']/a/@href").extract_first()
        title = prop.xpath(
            "./div[@class='card__body']/h3[@class='card__title']/a/text()").extract_first()
        price = prop.xpath(
            "./div[@class='card__body']/div[@class='card__footer card__footer--primary']/div[@class='card__price']/text()").extract_first()
        description = prop.xpath(
            "./div[@class='card__body']/div[@class='card__synopsis']/p/text()").extract_first()
        bedrooms = prop.xpath(
            "./div[@class='card__body']/div[@class='card__footer card__footer--primary']/div[@class='features features--inline']/ol[@class ='features__list']/li[@class ='features__item'][1]/div[@class='features__label']/text()").extract_first()

        yield scrapy.Request(
            url=property_url,
            callback=self.parse_property,
            meta={
                'title': title,
                'price': price,
                'description': description,
                'bedrooms': bedrooms,
            }
        )

def parse_property(self, response):

    title = response.meta["title"]
    price = response.meta["price"]
    description = response.meta["description"]
    bedrooms = response.meta["bedrooms"]

    images = response.xpath('//a[contains(@class, "gallery__link ")]/@href').getall()

    yield {'title': title, 'price':price, "description": description, 'bedrooms': bedrooms, 'bathrooms': bathrooms, 'garages': garages, 'images':images}
...