Home > Software design >  How can I get the first 3 results of a 'for' loop?
How can I get the first 3 results of a 'for' loop?

Time:10-30

Here is the working code:

import scrapy

class imdb_project(scrapy.Spider):
    name = 'imdb'
    start_urls = ['https://www.imdb.com/chart/top']

    def parse(self, response):
        for i in response.css('.titleColumn a'):
            movie_name = i.css('::text').get()
            movie_url = i.css('::attr(href)').get()
            dict = {'movie': movie_name}

            yield response.follow(movie_url, callback=self.parse_info, meta=dict)

    def parse_info(self, response):
        movie_name2 = response.meta['movie']
        duration = response.css('ul.dxizHm li:nth-child(3)::text').get()
        genre = ', '.join(response.css('a.ipc-chip--on-baseAlt *::text').getall())

        print('\n')
        yield {
            'Movie Name': movie_name2,
            'Duration': duration,
            'Genre': genre,
        }
        print('\n')

This shows me all 250 results but what if I only want see the first 3 results for example?

CodePudding user response:

Change your list iteration

for i in response.css('.titleColumn a'):    

to:

for i in response.css('.titleColumn a')[:3]:
  • Related