Home > Back-end >  Finding out how many scrollable images in a webpage
Finding out how many scrollable images in a webpage

Time:08-26

I have a webpage that contains images hich could be scrolled from left to right using arrow buttons.I am interested to find out how many pictures are available for scrolling in the webpage.Is there any way to do this in python.I have tried to read out a text in the webpage right above the pictures which says 1 von 20(1 of 20 in English) but was not successful.The highlighted part I am interested to read

Is there any solution to this problem or any other method to find out how many images available to scroll in a webpage.The website link,which is a german newspaper, is as followsWebpage with scrolableimages Any hint would be really appreciated.

CodePudding user response:

You create a little python script that uses https://www.crummy.com/software/BeautifulSoup/bs4/doc/ (for easily scraping html pages) and https://requests.readthedocs.io/en/latest/ (to download the webpage source into text format).

With that, you can then search for all the elements you need for this webpage.

I checked on your website and the gallery containing the images as a pretty basic html architecture. You can see that by right-clicking anywhere in the webpage and select Inspect.

It looks like all the images in the gallery possesses the class name park-gallery__item-innerwrapper and are of type a.

So with requests and BeautifulSoup you can do a little script like that in python:

import requests
from bs4 import BeautifulSoup

def count_images_in_gallery(url):

    r = requests.get(url)
    soup = BeautifulSoup(r.text, 'html.parser')

    images_in_gallery = soup.find_all('a', class_='park-gallery__item-innerwrapper')

    print(f'This web page {url} has got {len(images_in_gallery)} images.')



if __name__ == "__main__": 
    count_images_in_gallery('https://rp-online.de/sport/fussball/bundesliga/bundesliga-2022-23-die-reaktionen-der-trainer-am-3-spieltag_bid-51746275')

NB: The script may not working - but it's a good exemple of what you can do. You need to consult the BeautifulSoup documentation for further informations about the feature and what you can do for web-scrapping.

  • Related