I have the following Python3 code I use to scrape a website:
import scrapy
class MicrophonesSpider(scrapy.Spider):
name = 'microphones'
allowed_domains = ['www.marktplaats.nl']
start_urls = ['https://www.marktplaats.nl/l/muziek-en-instrumenten/microfoons']
def parse(self, response):
for ad in response.xpath("//ul[@class='mp-Listings mp-Listings--list-view']/li/a"):
yield {
'title' : ad.xpath("..//figure/div/img/@alt").get(),
'link' : ad.xpath("..//@href").get()
}
next_page = response.xpath("//nav[@class='mp-PaginationControls-pagination']//a[./span[contains(@class,'mp-svg-arrow-right--inverse')]]/@href").extract_first()
if next_page:
next_page = response.urljoin(next_page)
print('\n\n' + next_page) # this line is for debugging only
yield response.follow(url=next_page, callback=self.parse)
This code works fine, but it somehow only scrapes the first three webpages and stops after that, I'm trying to figure out why that is the case.
Becuase the response.xpath("//nav[@class='mp-PaginationControls-pagination']//a[./span[contains(@class,'mp-svg-arrow-right--inverse')]]/@href").get() line still has a valid match inside the HTML when I inspect the https://www.marktplaats.nl/l/muziek-en-instrumenten/microfoons/p/3/ webpage when I search for it inside the page source.
It also doens't gives an error it just says 2022-09-21 22:53:10 [scrapy.core.engine] INFO: Closing spider (finished) so it seems that it's not a bug.
Please note that I run the spider as scrapy crawl microphones within the Scrapy framework.
Thanks in advance.
