Я пытаюсь написать программу, которая отправляет запрос URL-адреса на сайт, который затем создает анимацию метеорологического радара. Затем я очищаю эту страницу, чтобы получить URL-адреса изображений (они хранятся в модуле Java) и загрузить их в локальную папку. Я делаю это итеративно над многими радиолокационными станциями и двумя радарными продуктами. До сих пор я написал код для отправки запроса, проанализировал html и перечислил URL-адреса изображений. То, что я не могу сделать, это переименовать и сохранить изображения локально. Помимо этого, я хочу сделать это как можно более упорядоченным, что, вероятно, НЕ то, что у меня есть до сих пор. Любая помощь 1) получение изображений для загрузки в локальную папку и 2) указание на более питонический способ сделать это было бы здорово.самый pythonic способ получить изображения изнутри модуля (в HTML)
# import modules
import urllib2
import re
from bs4 import BeautifulSoup
##test variables##
stationName = "KBYX"
prod = ("bref1","vel1") # a tupel of both ref and vel
bkgr = "black"
duration = "1"
#home_dir = "/path/to/home/directory/folderForImages"
##program##
# This program needs to do the following:
# read the folder structure from home directory to get radar names
#left off here
list_of_folders = os.listdir(home_dir)
for each_folder in list_of_folders:
if each_folder.startswith('k'):
print each_folder
# here each folder that starts with a "k" represents a radar station, and within each folder are two other folders bref1 and vel1, the two products. I want the program to read the folders to decide which radar to retrieve the data for... so if I decide to add radars, all I have to do is add the folders to the directory tree.
# first request will be for prod[0] - base reflectivity
# second request will be for prod[1] - base velocity
# sample path:
# http://weather.rap.ucar.edu/radar/displayRad.php?icao=KMPX&prod=bref1&bkgr=black&duration=1
#base part of the path
base = "http://weather.rap.ucar.edu/radar/displayRad.php?"
#additional parameters
call = base+"icao="+stationName+"&prod="+prod[0]+"&bkgr="+bkgr+"&duration="+duration
#read in the webpage
urlContent = urllib2.urlopen(call).read()
webpage=urllib2.urlopen(call)
#parse the webpage with BeautifulSoup
soup = BeautifulSoup(urlContent)
#print (soup.prettify()) # if you want to take a look at the parsed structure
tag = soup.param.param.param.param.param.param.param #find the tag that holds all the filenames (which are nested in the PARAM tag, and
# located in the "value" parameter for PARAM name="filename")
files_in=str(tag['value'])
files = files_in.split(',') # they're in a single element, so split them by comma
directory = home_dir+"/"+stationName+"/"+prod[1]+"/"
counter = 0
for file in files: # now we should THEORETICALLY be able to iterate over them to download them... here I just print them
print file
спасибо. Я проверю их - они выглядят многообещающими, просто потребуется немного времени, чтобы их полностью понять. – user2209220