- 论坛徽章:
- 0
|
初试Python,练习一下。
main.py
from math import fmod
from getNG import getNG
def autogetNG3(month, frm, to):
for day in range(frm, to+1):
getNG("2009", "%(#)02d"%{"#":month}, "%(#)02d"%{"#":day}, "0", "http://192.168.0.114:666/")
if __name__ == "__main__":
autogetNG3(7, 1, 31)
|
getNG.py
import os
##import httplib
import datetime
import urllib2
import string
## proxy format:
## "http":"http://user:passwd@host:port/"
##http://lava.nationalgeographic.com/cgi-bin/pod/wallpaper.cgi?day=03&month=06&year=06
##http://photography.nationalgeographic.com/ngs_pod_ext/searchPOD.jsp?month=01&day=30&year=2007&errorForwardPage=%2Fphotography%2Fphoto-of-the-day%2Farchive&page=
def getNG(year, month, day, proxyprot, proxy):
if proxyprot!="0":
print "Use proxy "+proxyprot+":"+proxy
print year+"-"+month+"-"+day
ngurl_main="http://photography.nationalgeographic.com"
##ngurl_main="photography.nationalgeographic.com"
##1st Req Get the image name
ngurl_req1="/ngs_pod_ext/searchPOD.jsp?"
ngurl_req1+="month="+month+"&day="+day+"&year="+year
##ngurl="www.python.org"
ngurl=ngurl_main+ngurl_req1
##print ngurl
##con=httplib.HTTPConnection(ngurl)
##con.request("","")
##resp=con.getresponse()
##print resp.status,resp.reason
##data=resp.read()
##print data
if proxyprot!="0":
proxy_handler=urllib2.ProxyHandler({proxyprot:proxy})
opener=urllib2.build_opener(proxy_handler)
print "opening \""+ngurl+"\"..."
if proxyprot=="0":
con=urllib2.urlopen(ngurl)
else:
req=urllib2.Request(ngurl,None,{'User-agent':
'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2)'})
con=opener.open(req)
urltmp=con.geturl()
print "url="+urltmp
print "getting image basename..."
image_name_base=urltmp.split('/').pop().split('.')[0]
print image_name_base
image_name_base=string.rstrip(image_name_base, "-pod")
print image_name_base
urltmp=ngurl_main+"/photography/wallpaper/"+image_name_base+".html"
##+"_pod_image.html"
print "opening \""+urltmp+"\"..."
try:
if proxyprot=="0":
con2=urllib2.urlopen(urltmp)
else:
req=urllib2.Request(urltmp,None,{'User-agent':
'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2)'})
con2=opener.open(req)
except urllib2.HTTPError:
print "can not open"
return
except:
print "other error"
exit()
data=con2.read()
##file_tmp=file("tmp.log","wb+")
##print "storing tmp.log",
##file_tmp.write(data)
##file_tmp.flush()
##file_tmp.close()
print "data parsing..."
idx_ref=string.find(data,"1280 x 1024")
print "found 1280 x 1024 @",idx_ref
if idx_ref == -1:
idx_ref=string.find(data,"1024 x 768")
print "found 1024 x 768 @",idx_ref
for i in range(10):
findhead=idx_ref-(i*20);
print "find in [",findhead,",",idx_ref,"]"
idx_target=string.find(data,"href",findhead,idx_ref)
if -1:break;
print "found target href @", idx_target
idx_quothead=string.find(data,"\"",idx_target)
print "found head quot @",idx_quothead
idx_quottail=string.find(data,"\"",idx_quothead+1)
print "found tail quot @",idx_quottail
ngurl_target=data[idx_quothead+1:idx_quottail]
print ngurl_target
ngurl=ngurl_main+ngurl_target
print "getting \"",ngurl,"\"..."
if proxyprot=="0":
con3=urllib2.urlopen(ngurl)
else:
req=urllib2.Request(ngurl,None,{'User-agent':
'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2)'})
con3=opener.open(req)
data_img=con3.read()
filename_img=year+"-"+month+"-"+day+".jpg"
file_img=file(filename_img,"wb+")
print "storing file @ ",filename_img
file_img.write(data_img)
file_img.flush()
file_img.close()
if __name__ == "__main__":
date1=raw_input("Enter the date you want(Enter = today)[yymmdd]:")
##date1="071125"
getNG("20"+date1[0:2], date1[2:4], date1[4:6], "http", "http://192.168.0.114:666/")
|
欢迎乱用。 |
-
-
NG.7z
1.73 KB, 下载次数: 144
打包下载
|