Я работал над проектом по поиску в сети на сайте недвижимости. Идея состоит в том, чтобы собрать цены и общую информацию обо всех свойствах на веб-странице.
Поскольку я использую Scrapy Framework в Jupyter Notebook, это мой код:
class QuotesSpider(scrapy.Spider):
name = "quotes"
link = 'https://www.vivareal.com.br/venda/sp/sao-paulo/?pagina={number}#onde=BR-Sao_Paulo-NULL-Sao_Paulo&tipos=apartamento_residencial'
start_urls = ['https://www.vivareal.com.br/venda/sp/sao-paulo/apartamento_residencial/#onde=BR-Sao_Paulo-NULL-Sao_Paulo&tipos=apartamento_residencial',
]
for i in range(1,3):
start_urls.append(link.format(number=i))
#print(start_urls)
start_urls = ['https://www.vivareal.com.br/venda/sp/sao-paulo/apartamento_residencial/#onde=BR-Sao_Paulo-NULL-Sao_Paulo&tipos=apartamento_residencial',
]
custom_settings = {
'LOG_LEVEL': logging.WARNING,
'ITEM_PIPELINES': {'__main__.JsonWriterPipeline': 1}, # Used for pipeline 1
'FEED_FORMAT':'json', # Used for pipeline 2
'FEED_URI': 'quoteresult.json', # Used for pipeline 2
'CLOSESPIDER_PAGECOUNT': 3
}
def parse(self, response):
display(response.body)
table_rows = response.css('div.property-card__main-content') #//*[@id="js-site-main"]/div[2]/div
for quote in table_rows:
time.sleep(0.2)
yield {
'address': quote.css('span.property-card__address::text').extract_first(),#.re(r'.*'),
'title': quote.css('a.property-card__title::text').extract_first(),
'area': quote.css('span.js-property-card-detail-area::text').extract_first(),
'price': quote.css('div.js-property-card__price-small::text').extract_first(),
'cond_price': quote.css('strong.js-condo-price::text').extract_first(),
'bedrooms': quote.css('span.property-card__detail-value::text').extract()[1],
'bathrooms': quote.css('span.property-card__detail-value::text').extract()[3],
'amenities': quote.css('.amenities__item::attr(title)').extract(),
#'pictures': quote.css('div.carousel__item-wrapper::text').extract()[2]
}
Это Код отлично работает на первой странице с 36 свойствами. Однако, когда он переходит на следующую страницу, веб-сайту требуется некоторое время для обновления содержимого, и я получаю испорченную версию веб-сайта (со смесью свойств с первой страницы и второй).
Я прочитал несколько примеров относительно следующего и дополнил свой код следующим
class QuotesSpider(scrapy.Spider):
name = "quotes"
link = 'https://www.vivareal.com.br/venda/sp/sao-paulo/?pagina={number}#onde=BR-Sao_Paulo-NULL-Sao_Paulo&tipos=apartamento_residencial'
start_urls = ['https://www.vivareal.com.br/venda/sp/sao-paulo/apartamento_residencial/#onde=BR-Sao_Paulo-NULL-Sao_Paulo&tipos=apartamento_residencial',
]
for i in range(1,3):
start_urls.append(link.format(number=i))
#print(start_urls)
start_urls = ['https://www.vivareal.com.br/venda/sp/sao-paulo/apartamento_residencial/#onde=BR-Sao_Paulo-NULL-Sao_Paulo&tipos=apartamento_residencial',
]
custom_settings = {
'LOG_LEVEL': logging.WARNING,
'ITEM_PIPELINES': {'__main__.JsonWriterPipeline': 1}, # Used for pipeline 1
'FEED_FORMAT':'json', # Used for pipeline 2
'FEED_URI': 'quoteresult.json', # Used for pipeline 2
'CLOSESPIDER_PAGECOUNT': 3
}
def parse(self, response):
display(response.body)
table_rows = response.css('div.property-card__main-content') #//*[@id="js-site-main"]/div[2]/div
for quote in table_rows:
time.sleep(0.2)
yield {
'address': quote.css('span.property-card__address::text').extract_first(),#.re(r'.*'),
'title': quote.css('a.property-card__title::text').extract_first(),
'area': quote.css('span.js-property-card-detail-area::text').extract_first(),
'price': quote.css('div.js-property-card__price-small::text').extract_first(),
'cond_price': quote.css('strong.js-condo-price::text').extract_first(),
'bedrooms': quote.css('span.property-card__detail-value::text').extract()[1],
'bathrooms': quote.css('span.property-card__detail-value::text').extract()[3],
'amenities': quote.css('.amenities__item::attr(title)').extract(),
#'pictures': quote.css('div.carousel__item-wrapper::text').extract()[2]
}
# next_page = /page-{}/ where {} number of page.
next_page = response.xpath('//*[@id="js-site-main"]/div[2]/div/section/div[2]/div[2]/div/ul/li[9]/a').extract_first()
# next_page = https://sanet.st/page-{}/ where {} number of page.
next_page = response.urljoin(next_page)
# If next_page have value
if next_page:
# Recall parse with url https://sanet.st/page-{}/ where {} number of page.
yield scrapy.Request(url=next_page, callback=self.parse)
Однако я получаю только результаты первой страницы и не могу понять, как ограничить количество страниц, которые я получаю, так как у меня есть нет доступа к счетчику l oop. Другими словами, как мне определить, когда это прекратится?
Спасибо вам