I'm trying to create a bot that downloads images from a website in python but it gives me an error in
image_url = image['src']
Here's the full code and the error
from PIL import Image
import requests
import bs4
def download_image(url):
response = requests.get(url)
soup = bs4.BeautifulSoup(response.text, "html.parser")
image = soup.find('img')
image_url = image['src']
img = Image.open(requests.get(image_url, stream=True).raw)
img.save('imageSaved.jpg')
print('success')
# download_image("https://apod.nasa.gov/apod/image/1701/potw1636aN159_HST_2048.jpg")
download_image("https://apod.nasa.gov/apod/random_apod.html")
The error:
Traceback (most recent call last):
File "/Users/fscozano/PycharmProjects/NFT_finalProject/main.py", line 21, in <module>
download_image("https://apod.nasa.gov/apod/random_apod.html")
File "/Users/fscozano/PycharmProjects/NFT_finalProject/main.py", line 11, in download_image
image_url = image['src']
TypeError: 'NoneType' object is not subscriptable
Please help me :) thx