Python - 从 URL 下载图像
Python - Download image from URL
我正在尝试下载以下图片:http://www.bom.gov.au/radar/IDR663.T.202202252249.png
或者显然有一个 FTP link: ftp://ftp.bom.gov.au/anon/gen/radar/
我尝试了以下方法,但 none 似乎有效:
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)
BOM 不允许网络抓取。您可以使用他们的 FTP 服务器。
http://www.bom.gov.au/catalogue/anon-ftp.shtml
请注意,雷达图像在删除前仅存储 1.2 小时。
此代码获取 /anon/gen/radar
目录中的所有文件名,然后仅使用 Brisbane/Mt Stapylton 文件制作 list
,最后仅下载并保存没有的文件'尚未下载并保存到磁盘。
您可以每小时 运行 获取所有新图像,然后再删除它们。
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()
我正在尝试下载以下图片:http://www.bom.gov.au/radar/IDR663.T.202202252249.png 或者显然有一个 FTP link: ftp://ftp.bom.gov.au/anon/gen/radar/
我尝试了以下方法,但 none 似乎有效:
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)
BOM 不允许网络抓取。您可以使用他们的 FTP 服务器。
http://www.bom.gov.au/catalogue/anon-ftp.shtml
请注意,雷达图像在删除前仅存储 1.2 小时。
此代码获取 /anon/gen/radar
目录中的所有文件名,然后仅使用 Brisbane/Mt Stapylton 文件制作 list
,最后仅下载并保存没有的文件'尚未下载并保存到磁盘。
您可以每小时 运行 获取所有新图像,然后再删除它们。
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()