def _last_page_number_in_search(response):
"""Get last page number of search results."""
try: # to get the last page number
last_page = response.xpath('//ul[@data-id="SearchResultsPagination"]/li[last()-1]/a/div/text()').extract()
return int(last_page[0])
except IndexError: # if there is no page number
# get the reason from the page
reason = response.xpath('//p[@class="text-lead"]/text()').extract()
# and if it contains the key words set last page equal to 0
if reason and ('find any results that matched your criteria' in reason[0]):
logging.log(logging.DEBUG, 'No results on page' + response.url)
return 0
else:
# otherwise we can conclude that the page
# has results but that there is only one page.
return 1
评论列表
文章目录