【发布时间】:2020-09-02 08:02:50
【问题描述】:
但是,它只能抓取前 4 页,然后就停止了 代码如下:
def parse(self, response):
# follow links to property pages
for href in response.xpath('//div[@class="listings-item-bottom"]//a[@class="link link--minimal"]/@href').getall():
yield response.follow(href, self.parse_property)
# follow pagination links
old_offset = self.page_counter
old_offset = str(old_offset) + '0' if old_offset != 0 else str(old_offset)
try:
max_page = int(''.join(response.css('div.custom-pagination-select::text').re(r'\d+')))
except:
max_page = None
self.page_counter += 1
if self.page_counter < max_page:
new_offset = str(self.page_counter) + '0'
next_page_url = response._get_url().replace(f'offset={old_offset}', f'offset={new_offset}')
next_page = response.urljoin(next_page_url)
yield scrapy.Request(next_page, callback=self.parse)
有人对这里可能出现的问题有任何建议吗? 提前谢谢!
【问题讨论】:
标签: python xpath web-scraping scrapy web-crawler