【发布时间】:2019-07-03 10:36:54
【问题描述】:
我一直在抓取这个网站并尝试存储属性,虽然有些属性确实被抓取,但有些只是被抓取而不是抓取:
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)
任何建议如何使这项工作? 提前非常感谢您
【问题讨论】:
标签: python web-scraping scrapy