免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 4900 | 回复: 7
打印 上一主题 下一主题

批量下载国家地理壁纸Python脚本 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-08-11 11:23 |只看该作者 |倒序浏览
初试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

打包下载

论坛徽章:
0
2 [报告]
发表于 2009-08-11 12:58 |只看该作者
帖源码就把一些无用或者测试的注释删了把。

论坛徽章:
0
3 [报告]
发表于 2009-08-11 17:31 |只看该作者
下图片去

论坛徽章:
0
4 [报告]
发表于 2009-08-11 20:41 |只看该作者
很好!非常好!

论坛徽章:
0
5 [报告]
发表于 2009-08-12 09:32 |只看该作者
谢谢分享,顶一下!

论坛徽章:
0
6 [报告]
发表于 2009-08-12 10:07 |只看该作者
同志们谁把它改进了也请发出来共享下。:)

论坛徽章:
0
7 [报告]
发表于 2010-02-07 16:55 |只看该作者
很好用,very good

论坛徽章:
0
8 [报告]
发表于 2010-02-13 02:28 |只看该作者
太好了
不过有bug
2010年的不能用
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP