无法提取类值

时间:2019-08-06 20:49:42

标签: python web-scraping scrapy

我一直在尝试从此网站上抓取信息: “ https://www.privateproperty.co.za/for-sale/western-cape/cape-town/55

具体来说,我在尝试获得卧室,浴室和车库的数量方面遇到问题。

现在,我的索引超出范围错误,我不确定如何解决此问题。有什么建议吗?

       for prop in response.css('div.resultsItemsContainer a'):
           link = 'https://www.privateproperty.co.za' + prop.css('::attr(href)').get()
           title = prop.css('div.title::text').get()
           price = prop.css('div.priceDescription::text').re(r'\d+')

           bedrooms, bathrooms, garages = None, None, None
           for i in range(len(prop.css('div.features.row div::attr(class)'))):
               counter = i - 1
               if prop.css('div.features.row div::attr(class)')[i].get() == 'icon bedroom':
                   bedrooms = prop.css('div.features.row div::text')[counter].get()
               elif prop.css('div.features.row div::attr(class)')[i].get() == 'icon bathroom':
                   bathrooms = prop.css('div.features.row div::text')[counter].get()
               elif prop.css('div.features.row div::attr(class)')[i].get() == 'icon garage':
                   garages = prop.css('div.features.row div::text')[counter].get()

示例错误:

    garages = prop.css('div.features.row div::text')[counter].get()
  File "/opt/anaconda2/lib/python2.7/site-packages/parsel/selector.py", line 61, in __getitem__
    o = super(SelectorList, self).__getitem__(pos)
IndexError: list index out of range

谢谢!

1 个答案:

答案 0 :(得分:1)

使用XPath非常简单:

.write()