#! /usr/bin/python# encoding=utf-8 "" "@desc download Beautiful wallpaper in momentum @api https://d3cbihxaqsuq0s.cloudfront.net/@author Tan < [email protected]> "" "Import requestsimport reimport osimport urllibimport xml.dom.minidomdef Download (URL, Path = '): Name = Url.split ('/') [ -1:-2:-1] path = path if path else Os.path.join ('./tmp/', fname if fname els E name) dirname = os.path.dirname (path) print path if not os.path.exists (dirname): Os.makedirs (dirname) Try:return urllib.urlretrieve (URL, path) except Exception,e:print "Error:", E return falsedef p Arsexmlfile (name): DOM = xml.dom.minidom.parse (name) List = dom.documentelement if List.hasattribute ("xmlns"): Print "Listbucketresult:%s"% list.getattribute ("xmlns") results = [] # gets all contents contents = List in the collection . getElementsByTagName ("Contents") # Print detailed information for each Content for C in Contents:print "*****content*****" Key = C.getelementsbytagname (' Key ') [0] #print "Key:%s"% key.childnodes[0].data lastmodified = c.getelementsbytagname (' lastmodified ') [0] #p Rint "LastModified:%s"% lastmodified.childnodes[0].data ETag = c.getelementsbytagname (' ETag ') [0] #print "E Tag:%s "% etag.childnodes[0].data size = c.getelementsbytagname (' size ') [0] #print" size:%s "% Size.childno Des[0].data Storageclass = c.getelementsbytagname (' storageclass ') [0] #print "storageclass:%s"% Storageclas S.childnodes[0].data item = {"Key": Key.childnodes[0].data, "ETag": Etag.childnodes[0].da TA, "size": Size.childnodes[0].data, "lastmodified": Lastmodified.childnodes[0].data, " Storageclass ": Storageclass.childnodes[0].data} results.append (item) return RESULTSDEF main (): TMP = './tmp/'; #下载xml path = Os.path.join (tmp, ' contents.xml ') if not download (' https://d3cbihxaqsuq0s.cloudfront.net/', path): Return False #解析xml并下载图片 contents = parsexmlfile (path) If Len (contents): For item in Contents:print Item if not item[' key '].find ('. jpg '): Continue path = Os.path.join (tmp, item[' key ') Download (' https://d3cbihxaqsuq0s.cloudfront.net/' + item[' key '], path) return trueif __name__ = = ' __main__ ': Main ()
Download Beautiful wallpaper from momentum in Python