How to crawl certain amount of images from a website

Question:

I have this code where I want to crawl images from a given website

from bs4 import *
import requests as rq
import os
import sys

page_url = sys.argv[1]
crawl = str(page_url)
r2 = rq.get('https://www.' + crawl + '' + '/')
soup2 = BeautifulSoup(r2.text, "html.parser")
images = []


image_sources = soup2.select('img')
for img in image_sources:
    images.append(img['src'])

for l in images:
    print(l)

how can a crawl for example only 15 images?

Answers:

To get max 15 images you can do:

...

for img in image_sources[:15]: # <--- max. 15 images
    images.append(img['src'])

...
Answered By: Andrej Kesely
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.