前回の準備編 に続いて、今回はコーディングに移りますが、まず最初に完成形となるコードを掲載します。
# coding: UTF-8
import os
import datetime
import mechanize
import urllib
# Set directory name.
directory_name = "images"
if not os.path.isdir(directory_name):
os.mkdir(directory_name)
# Set dates.
start_date = datetime.date(2005, 1, 1)
end_date = datetime.date(2010, 12, 31)
current_date = start_date
while current_date <= end_date:
print current_date
browser = mechanize.Browser()
browser.set_handle_robots(False)
# Open web page.
response = browser.open("http://argo.colorado.edu/~realtime/modis/")
# print "=== HTML Source ==="
# print response.read()
# Check forms.
# print "\n=== Fomrs ==="
# for form in browser.forms():
# print form
# Select first form.
browser.select_form(nr=0)
# Set day.
browser["month"] = [current_date.strftime("%m")]
browser["day1"] = [current_date.strftime("%d")[0]]
browser["day2"] = [current_date.strftime("%d")[1]]
browser["year"] = [current_date.strftime("%y")]
# Set location.
browser["alon0"] = "135"
browser["alon1"] = "160"
browser["alat0"] = "30"
browser["alat1"] = "45"
# Set options.
browser["day_opt"] = ["7"]
browser["cont_opt"] = ["T"]
browser["cont_anot"] = ["T"]
# Check forms.
# print "\n=== Forms ==="
# for form in browser.forms():
# print form
# Submit.
response = browser.submit()
# print "=== HTML Source ==="
# print response.read()
# Check links.
# print "\n=== Links ==="
for link in browser.links():
# print link
if link.url[-4:] == ".gif":
print link.url
urllib.urlretrieve(link.url, directory_name + "/" + current_date.isoformat() + ".gif")
current_date += datetime.timedelta(7)
print "Ended."
ここからはこのコードを目指して、どういった手順で書いていくかを説明します。 続きを読む →