没有使用xpath和Scrapy从div类中获取所有a元素

时间:2019-07-07 09:11:54

标签: python xpath web-scraping scrapy web-crawler

我一直在尝试从该网站获取所有属性。 当我在主搜索页面上访问所有这些属性时,我可以从所有属性中检索所有信息,但是,当我需要实际属性链接中的信息时,它似乎只能通过一个属性链接。

主要问题在链接部分,因此当我实际尝试访问属性的链接时。我只能从第一个属性获得链接和信息,而不能从其他所有属性获得信息。

class PropDataSpider(scrapy.Spider):
    name = "remax"
    start_urls = ['https://www.remax.co.za/property_search/for-sale/?minprice=100000&maxprice=1000000000&displayorder=date&cities=432']


    def parse(self, response):

        propertes = response.xpath("//div[@class='w-container main-content remodal-bg']")
        for prop in propertes:
            link = 'http://www.remax.co.za/' + prop.xpath("./a/@href").extract_first()
            agency = self.name
            title = prop.xpath(
                ".//div[@class='property-item']/div[@class='w-clearfix']/p[@class='property-type']/text()").extract_first().strip()
            price = prop.xpath(
                 ".//div[@class='property-item']/div[@class='w-clearfix']/div/strong/text()").extract_first().strip()

...


           yield scrapy.Request(
                link,
                callback=self.parse_property,
                meta={
                    'agency': agency,
                    'title': title,
                    'price': price,
                    'description': description,
                    'bedrooms': bedrooms,
                    'bathrooms': bathrooms,
                    'garages': garages,
                }
            )


 def parse_property(self, response):
        agency = response.meta["agency"]
        title = response.meta["title"]
        price = response.meta["price"]
        description = response.meta["description"]
        bedrooms = response.meta["bedrooms"]
        bathrooms = response.meta["bathrooms"]
        garages = response.meta["garages"]


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

我想获得的所有其他指向属性的链接。我不确定自己在做什么错以及如何解决这个问题。

非常感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

您需要进行一些更改:

$ pub downgrade stagehand 3.3.1