简体   繁体   中英

How can I get the first 3 results of a 'for' loop?

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?

Change your list iteration

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

to:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM