简体   繁体   中英

How to crawl certain amount of images from a website

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?

To get max 15 images you can do:

...

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

...

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