Skip to content
Advertisement

How to check if there is a picture on a website or not with Python and Selenium

I want to check with a boolean request, if there is a picture on the website:

https://portal.dnb.de/opac/mvb/cover?isbn=9783442472352

or not:

https://portal.dnb.de/opac/mvb/cover?isbn=3499239663

I don`t know how that is possible.

Thank you for your help!!

Advertisement

Answer

Looks like you should deal with response status – for selenium, take a read:

How to get status code by using selenium.py (python code)

Alternative approach, get the status with requests:

import requests
urls = ['https://portal.dnb.de/opac/mvb/cover?isbn=9783442472352','https://portal.dnb.de/opac/mvb/cover?isbn=3499239663']
for url in urls:
    r = requests.get(url)
    if r.status_code == 200:
        print(url, ': yeah, there seems to be an image')
    elif r.status_code == 400:
        print(url, ': nope, no image here')

Output

https://portal.dnb.de/opac/mvb/cover?isbn=9783442472352 : yeah, there seems to be an image
https://portal.dnb.de/opac/mvb/cover?isbn=3499239663 : nope, no image here
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement