直接上代码吧,比较简单:
#-*- encoding:utf-8 -*-'''自动下载壁纸,太平洋电脑网壁纸,http://wallpaper.pconline.com.cnBy garfieldtom,2012python ver:python 3感谢 枫叶饭团 提供解答,软件取回内容有压缩'''import re,urllib.requestimport gzip#获得页面内容def downwallpapers(url): urlcontent=geturlcontent(url) imglist=getimglist(urlcontent) for img in imglist: downimg(img) def geturlcontent(url): #返回页面内容 doc = urllib.request.urlopen(url).read() #解码,有些是压缩的,或者说有时是压缩的,随机??没搞懂 try: html=gzip.decompress(doc).decode("gbk") except: html=doc.decode("gbk") return html#下载图片def downimg(url): imgdir ='/home/xxh/图片/wallpapers/' filename = imgdir + url.split("/")[-1] print("准备下载................") print("url:"+url) print("filename:"+filename) urllib.request.urlretrieve(url, filename) #取图片链接def getimglist(doc): #取照图图片地址 regImg = '
再次感谢 枫叶饭团 提供解答,软件取回内容有压缩 !