I'm trying to download the following image: http://www.bom.gov.au/radar/IDR663.T.202202252249.png Alternatively there's apparently an FTP link: ftp://ftp.bom.gov.au/anon/gen/radar/
I've tried the following but none of it seems to work:
def download_image(url):
img_data = requests.get(url).content
with open('image_name.png', 'wb') as handler:
handler.write(img_data)
def download_image_2(url):
with open('image_name.png', 'wb') as handle:
response = requests.get(url, stream=True)
if not response.ok:
print(response)
for block in response.iter_content(1024):
if not block:
break
handle.write(block)
def download_image_3(url):
response = requests.get(url)
if not response.ok:
print(response)
file = open("image_name.png", "wb")
file.write(response.content)
file.close()
def download_image_4(url):
# Downloading from an FTP stream
with closing(req.urlopen(url)) as r:
with open('image_name.png', 'wb') as f:
shutil.copyfileobj(r, f)
CodePudding user response:
BOM doesn't allow webscraping. You can use their FTP server.
http://www.bom.gov.au/catalogue/anon-ftp.shtml
Note the radar images are only stored for 1.2 hours before being deleted.
This code gets all the filenames in the /anon/gen/radar
directory, then makes a list
with only the Brisbane/Mt Stapylton files, and finally only downloads and saves the files which haven't yet been downloaded and saved to disk.
You could run this every hour to get all the new images before they are deleted.
import os
from ftplib import FTP
def get_brisbane_filenames(filename):
if filename.startswith("IDR663.T."):
brisbane_filenames.append(filename)
ftp = FTP(r"ftp.bom.gov.au")
ftp.login()
ftp.cwd("/anon/gen/radar")
brisbane_filenames = []
ftp.retrlines("NLST", get_brisbane_filenames)
for filename in brisbane_filenames:
if not os.path.isfile(filename):
with open(filename, "wb") as fp:
ftp.retrbinary(f"RETR {filename}", fp.write)
print(f"Downloaded and saved {filename}")
ftp.quit()