温馨提示:本文翻译自stackoverflow.com,查看原文请点击:python - Websites getting crawled but not scraped Scrapy

python - 网站被抓取但未被抓取Scrapy

发布于 2020-03-27 10:21:18

我一直在抓取此网站并尝试存储属性,而某些属性确实被抓取了,而有些只是被抓取而未被抓取:

class CapeWaterfrontSpider(scrapy.Spider):
    name = "cape_waterfront"
    start_urls = ['https://www.capewaterfrontestates.co.za/template/Properties.vm/listingtype/SALES']

    def parse(self, response):
        for prop in response.css('div.col-sm-6.col-md-12.grid-sizer.grid-item'):

            link = prop.css('div.property-image a::attr(href)').get()

            bedrooms = prop.css('div.property-details li.bedrooms::text').getall()
            bathrooms = prop.css('div.property-details li.bathrooms::text').getall()
            gar = prop.css('div.property-details li.garages::text').getall()

            if len(bedrooms) == 0:
                bedrooms.append(None)
            else:
                bedrooms = bedrooms[1].split()
            if len(bathrooms) == 0:
                bathrooms.append(None)
            else:
                bathrooms = bathrooms[1].split()
            if len(gar) == 0:
                gar.append(None)
            else:
                gar = gar[1].split()

            yield scrapy.Request(
                link,
                meta={'item': {
                    'agency': self.name,
                    'url': link,
                    'title': ' '.join(prop.css('div.property-details p.intro::text').get().split()),
                    'price': ''.join(prop.css('div.property-details p.price::text').get().split()),
                    'bedrooms': str(bedrooms),
                    'bathroom':  str(bathrooms),
                    'garages': str(gar)
                }},
                callback=self.get_loc,
            )

        next_page = response.css('p.form-control-static.pagination-link a::attr(href)').get()
        if next_page is not None:
            next_page = response.urljoin(next_page)
            yield scrapy.Request(next_page, callback=self.parse)

有什么建议可以使这项工作吗?提前非常感谢你

查看更多

查看更多

提问者
saraherceg
被浏览
145
SIM 2019-07-03 22:41

您定义选择器的方式容易出错。此外,几乎没有故障的选择器很少起作用。指向下一页的链接也无法正常工作。它只会转到第1页,然后退出。最后,我不知道next_siblingcss选择器中的任何用法,因此我不得不以某种尴尬的方式来挖掘下一个同级对象。

class CapeWaterfrontSpider(scrapy.Spider):
    name = "cape_waterfront"
    start_urls = ['https://www.capewaterfrontestates.co.za/template/Properties.vm/listingtype/SALES']

    def parse(self, response):

        for prop in response.css('.grid-item'):
            link = prop.css('.property-image a::attr(href)').get()

            bedrooms = [elem.strip() for elem in prop.css(".bedrooms::text").getall()]
            bedrooms = bedrooms[-2] if len(bedrooms)>=1 else None

            bathrooms = [elem.strip() for elem in prop.css(".bathrooms::text").getall()]
            bathrooms = bathrooms[-2] if len(bathrooms)>=1 else None

            gar = [elem.strip() for elem in prop.css(".garages::text").getall()]
            gar = gar[-2] if len(gar)>=1 else None

            yield scrapy.Request(
                link,
                meta={'item': {
                    'agency': self.name,
                    'url': link,
                    'bedrooms': bedrooms,
                    'bathroom':  bathrooms,
                    'garages': gar
                }},
                callback=self.get_loc,
            )

        next_page = response.css('.pagination-link a.next::attr(href)').get()
        if next_page:
            next_page = response.urljoin(next_page)
            yield scrapy.Request(next_page, callback=self.parse)

    def get_loc(self,response):
        items = response.meta['item']
        print(items)

如果您想采用一种更清洁的方法来获得这三个项目,我想xpath您要坚持的是:

for prop in response.css('.grid-item'):
    link = prop.css('.property-image a::attr(href)').get()
    bedrooms = prop.xpath("normalize-space(.//*[contains(@class,'bedrooms')]/label/following::text())").get()
    bathrooms = prop.xpath("normalize-space(.//*[contains(@class,'bathrooms')]/label/following::text())").get()
    gar = prop.xpath("normalize-space(.//*[contains(@class,'garages')]/label/following::text())").get()

为了简洁起见,我已经淘汰了两三个字段,并且我想您可以对其进行管理。

发布
问题

分享
好友

手机
浏览

扫码手机浏览