Last active
May 3, 2020 21:36
-
-
Save Ludorg/8693f43548042aaf1cf70a7a2f6d5b99 to your computer and use it in GitHub Desktop.
Get NASA "Astronomy Picture of the Day"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Get "Astronomy Picture of the Day" automatically from https://apod.nasa.gov/ | |
# Saved file is today-date(YYYY-MM-DD) + filename (example: 2020-04-28_Kepler90Illustration_Kepler_1080.jpg) | |
# Requires: | |
# - lxml | |
# - requests | |
# - beautifulsoup4 | |
import requests | |
from bs4 import BeautifulSoup | |
from datetime import date | |
url = 'https://apod.nasa.gov/apod/' | |
response = requests.get(url) | |
s = BeautifulSoup(response.text, 'lxml') | |
# <a href="image/2005/Message_Arecibo_2000.jpg"> | |
# <IMG SRC="image/2005/Message_Arecibo_960.jpg" | |
# alt="See Explanation. Clicking on the picture will download the highest resolution version available." style="max-width:100%"></a> | |
# find <img ...> | |
imgRoot = s.find('img') | |
# take the parent | |
print(imgRoot.parent) | |
# get img link target (a href) not the one displayed in img src | |
src = imgRoot.parent.get('href') | |
img_url = url + src | |
print(img_url) | |
print(date.today()) | |
filename = date.today().strftime('%Y-%m-%d') + '_' + src.split('/')[-1] | |
print(filename) | |
img_response = requests.get(img_url) | |
if img_response.status_code == 200: | |
with open(filename, 'wb') as f: | |
f.write(img_response.content) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment