0

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

  • The page is dynamically generated. You cannot use `requests` to download it. You need selenium. – DYZ Dec 21 '21 at 19:29
  • Adding onto DYZ's answer. You could also use a proxy service that is capable of rendering javascript. Scrapfly.io is an example. These are paid services though. I am not affiliated with them feel free to research your own. Or use selenium. – mr_mooo_cow Dec 21 '21 at 21:22

0 Answers0