Jan-29-2022, 03:25 PM
(This post was last modified: Jan-29-2022, 03:25 PM by bananatoast.)
I'm writing my first program to download images from a website and have run into a roadblock.
I can't seem to get the src attribute in the variable 'img_url' which contains the div that holds it.
after run the code i get none in the print result instead of the url
<div id="comic">
<img alt="Barrel - Part 1" src="//imgs.xkcd.com/comics/barrel_cropped_(1).jpg" style="image-orientation:none" title="Don't we all."/>
</div>
I can't seem to get the src attribute in the variable 'img_url' which contains the div that holds it.
after run the code i get none in the print result instead of the url
import requests
from bs4 import BeautifulSoup
count = 1
url = f'https://xkcd.com/{count}/'
file_name = f'savedimage0{count}.png'
while True:
page = requests.get(url)
print(f"Status code: {page.status_code} - page read successfully!")
soup = BeautifulSoup(page.content, 'html.parser')
img_url = soup.find(id='comic')
img_url = img_url.get('src')
print(img_url)when i print img_url before using img_url.get('src') this is what displays:<div id="comic">
<img alt="Barrel - Part 1" src="//imgs.xkcd.com/comics/barrel_cropped_(1).jpg" style="image-orientation:none" title="Don't we all."/>
</div>
